feat: override spring bean
This commit is contained in:
parent
9cea810493
commit
47c7554352
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.overridebean;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class Endpoint {
|
||||||
|
|
||||||
|
private final Service service;
|
||||||
|
|
||||||
|
public Endpoint(Service service) {
|
||||||
|
this.service = service;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/hello")
|
||||||
|
public String helloWorldEndpoint() {
|
||||||
|
return service.helloWorld();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.overridebean;
|
||||||
|
|
||||||
|
public interface Service {
|
||||||
|
String helloWorld();
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.overridebean;
|
||||||
|
|
||||||
|
public class ServiceImpl implements Service {
|
||||||
|
|
||||||
|
public String helloWorld() {
|
||||||
|
return "hello world";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.overridebean.basic;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
|
|
||||||
|
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, OAuth2ResourceServerAutoConfiguration.class })
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baeldung.overridebean.basic;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
import com.baeldung.overridebean.ServiceImpl;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Service helloWorld() {
|
||||||
|
return new ServiceImpl();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.overridebean.conditional;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
|
|
||||||
|
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, OAuth2ResourceServerAutoConfiguration.class })
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.overridebean.conditional;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
import com.baeldung.overridebean.ServiceImpl;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ConditionalConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnProperty(name = "service.stub", havingValue = "false")
|
||||||
|
public Service helloWorld() {
|
||||||
|
return new ServiceImpl();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.overridebean.profile;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
|
|
||||||
|
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, OAuth2ResourceServerAutoConfiguration.class })
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.overridebean.profile;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
import com.baeldung.overridebean.ServiceImpl;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Profile("prod")
|
||||||
|
public class ProfileConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Service helloWorld() {
|
||||||
|
return new ServiceImpl();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,2 +1,3 @@
|
||||||
keycloak.enabled=true
|
keycloak.enabled=true
|
||||||
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8180/auth/realms/baeldung-api
|
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8180/auth/realms/baeldung-api
|
||||||
|
service.stub=false
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.overridebean.conditional;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Endpoint;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = { Application.class, ConditionalConfig.class, Endpoint.class, ConditionalTestConfig.class }, properties = "service.stub=true")
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
class ConditionIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenConditionalConfig_whenServiceStubIsTrue_thenStubOk() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/hello"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string(containsString("hello conditional stub")));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.overridebean.conditional;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
|
||||||
|
public class ConditionalStub implements Service {
|
||||||
|
|
||||||
|
public String helloWorld() {
|
||||||
|
return "hello conditional stub";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.overridebean.conditional;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
|
||||||
|
@TestConfiguration
|
||||||
|
public class ConditionalTestConfig {
|
||||||
|
|
||||||
|
@ConditionalOnProperty(name = "service.stub", havingValue = "true")
|
||||||
|
@Bean
|
||||||
|
public Service helloWorld() {
|
||||||
|
return new ConditionalStub();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.baeldung.overridebean.mockbean;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Endpoint;
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
import com.baeldung.overridebean.basic.Application;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = { Application.class, Endpoint.class })
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
class MockBeanIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private Service serviceExample;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenServiceMockBean_whenGetHelloEndpoint_thenMockOk() throws Exception {
|
||||||
|
when(serviceExample.helloWorld()).thenReturn("hello mock bean");
|
||||||
|
this.mockMvc.perform(get("/hello"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string(containsString("hello mock bean")));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.overridebean.overridebeandefinition;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Endpoint;
|
||||||
|
import com.baeldung.overridebean.basic.Application;
|
||||||
|
import com.baeldung.overridebean.basic.Config;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = { Application.class, Config.class, Endpoint.class, OverrideBeanDefinitionTestConfig.class }, properties = "spring.main.allow-bean-definition-overriding=true")
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
class OverrideBeanDefinitionIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNoProfile_whenAllowBeanDefinitionOverriding_thenStubOk() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/hello"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string(containsString("hello no profile stub")));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.overridebean.overridebeandefinition;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
|
||||||
|
public class OverrideBeanDefinitionServiceStub implements Service {
|
||||||
|
|
||||||
|
public String helloWorld() {
|
||||||
|
return "hello no profile stub";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.overridebean.overridebeandefinition;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
|
||||||
|
@TestConfiguration
|
||||||
|
public class OverrideBeanDefinitionTestConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Service helloWorld() {
|
||||||
|
return new OverrideBeanDefinitionServiceStub();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.overridebean.primary;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Endpoint;
|
||||||
|
import com.baeldung.overridebean.basic.Application;
|
||||||
|
import com.baeldung.overridebean.basic.Config;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = { Application.class, Config.class, Endpoint.class, PrimaryTestConfig.class })
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
class PrimaryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenTestConfiguration_whenPrimaryBeanIsDefined_thenStubOk() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/hello"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string(containsString("hello primary stub")));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.overridebean.primary;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
|
||||||
|
public class PrimaryServiceStub implements Service {
|
||||||
|
|
||||||
|
public String helloWorld() {
|
||||||
|
return "hello primary stub";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.overridebean.primary;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
|
||||||
|
@TestConfiguration
|
||||||
|
public class PrimaryTestConfig {
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean("service.stub")
|
||||||
|
public Service helloWorld() {
|
||||||
|
return new PrimaryServiceStub();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.overridebean.profile;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Endpoint;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = { Application.class, ProfileConfig.class, Endpoint.class, ProfileTestConfig.class })
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
class ProfileIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenConfigurationWithProfile_whenNoProductionProfileIsActive_thenStubOk() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/hello"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string(containsString("hello profile stub")));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.overridebean.profile;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
|
||||||
|
public class ProfileServiceStub implements Service {
|
||||||
|
|
||||||
|
public String helloWorld() {
|
||||||
|
return "hello profile stub";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.overridebean.profile;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
import com.baeldung.overridebean.Service;
|
||||||
|
|
||||||
|
@TestConfiguration
|
||||||
|
public class ProfileTestConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Service helloWorld() {
|
||||||
|
return new ProfileServiceStub();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue