diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/conditional/ConditionalTestConfig.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/conditional/ConditionalTestConfig.java index c48c4e2266..ce82b43df0 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/conditional/ConditionalTestConfig.java +++ b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/conditional/ConditionalTestConfig.java @@ -9,8 +9,8 @@ import com.baeldung.overridebean.Service; @TestConfiguration public class ConditionalTestConfig { - @ConditionalOnProperty(name = "service.stub", havingValue = "true") @Bean + @ConditionalOnProperty(name = "service.stub", havingValue = "true") public Service helloWorld() { return new ConditionalStub(); } diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/mockbean/MockBeanIntegrationTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/mockbean/MockBeanIntegrationTest.java index 2ece28482d..9a63ad4113 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/mockbean/MockBeanIntegrationTest.java +++ b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/mockbean/MockBeanIntegrationTest.java @@ -25,11 +25,11 @@ class MockBeanIntegrationTest { private MockMvc mockMvc; @MockBean - private Service serviceExample; + private Service service; @Test void givenServiceMockBean_whenGetHelloEndpoint_thenMockOk() throws Exception { - when(serviceExample.helloWorld()).thenReturn("hello mock bean"); + when(service.helloWorld()).thenReturn("hello mock bean"); this.mockMvc.perform(get("/hello")) .andExpect(status().isOk()) .andExpect(content().string(containsString("hello mock bean"))); diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationMockTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationMockTest.java new file mode 100644 index 0000000000..43fb69860d --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationMockTest.java @@ -0,0 +1,38 @@ +package com.baeldung.overridebean.profile; + +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.test.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; + +import com.baeldung.overridebean.Endpoint; +import com.baeldung.overridebean.Service; +import com.baeldung.overridebean.boot.Application; + +@SpringBootTest(classes = { Application.class, ProfileConfig.class, Endpoint.class, ProfileTestConfig.class }) +@AutoConfigureMockMvc +@ActiveProfiles("mock") +class ProfileIntegrationMockTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private Service service; + + @Test + void givenConfigurationWithProfile_whenTestProfileIsActive_thenMockOk() throws Exception { + when(service.helloWorld()).thenReturn("hello profile mock"); + this.mockMvc.perform(get("/hello")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("hello profile mock"))); + } +} diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationStubTest.java similarity index 84% rename from spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationTest.java rename to spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationStubTest.java index 7f4aae026c..e1c88f2a31 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationTest.java +++ b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileIntegrationStubTest.java @@ -9,6 +9,7 @@ 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.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import com.baeldung.overridebean.Endpoint; @@ -16,13 +17,14 @@ import com.baeldung.overridebean.boot.Application; @SpringBootTest(classes = { Application.class, ProfileConfig.class, Endpoint.class, ProfileTestConfig.class }) @AutoConfigureMockMvc -class ProfileIntegrationTest { +@ActiveProfiles("stub") +class ProfileIntegrationStubTest { @Autowired private MockMvc mockMvc; @Test - void givenConfigurationWithProfile_whenNoProductionProfileIsActive_thenStubOk() throws Exception { + void givenConfigurationWithProfile_whenTestProfileIsActive_thenStubOk() throws Exception { this.mockMvc.perform(get("/hello")) .andExpect(status().isOk()) .andExpect(content().string(containsString("hello profile stub"))); diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileTestConfig.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileTestConfig.java index edd33162e0..7e1de309d3 100644 --- a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileTestConfig.java +++ b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/overridebean/profile/ProfileTestConfig.java @@ -1,7 +1,10 @@ package com.baeldung.overridebean.profile; +import static org.mockito.Mockito.mock; + import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Profile; import com.baeldung.overridebean.Service; @@ -9,7 +12,14 @@ import com.baeldung.overridebean.Service; public class ProfileTestConfig { @Bean - public Service helloWorld() { + @Profile("stub") + public Service helloWorldStub() { return new ProfileServiceStub(); } + + @Bean + @Profile("mock") + public Service helloWorldMock() { + return mock(Service.class); + } }