clean, add mock example

This commit is contained in:
luca 2023-11-14 10:28:22 +01:00
parent 84f12d9c87
commit f20a18ed9b
5 changed files with 56 additions and 6 deletions

View File

@ -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();
}

View File

@ -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")));

View File

@ -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")));
}
}

View File

@ -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")));

View File

@ -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);
}
}