Java 正确配置SpringJUnit5测试配置
使用spring boot和junit5编写单元测试的过程中遇到了不少困难。 我读了很多文章和一些junit5文档,但没有找到任何有用的东西(或者我只是个盲人) 测试: } 配置(这是一个只获取某些类的实验,但我失败了): 在执行测试时,我假设全局上下文已经启动,尽管我指定在@Configuration类中只加载它的一部分 我的问题是如何控制和设置spring引导和junit5测试的测试上下文的精确配置 事实上,如果有人能提供一些关于组织本地测试框架的最佳实践的参考资料,我将非常感激。Java 正确配置SpringJUnit5测试配置,java,spring-boot,testing,junit5,Java,Spring Boot,Testing,Junit5,使用spring boot和junit5编写单元测试的过程中遇到了不少困难。 我读了很多文章和一些junit5文档,但没有找到任何有用的东西(或者我只是个盲人) 测试: } 配置(这是一个只获取某些类的实验,但我失败了): 在执行测试时,我假设全局上下文已经启动,尽管我指定在@Configuration类中只加载它的一部分 我的问题是如何控制和设置spring引导和junit5测试的测试上下文的精确配置 事实上,如果有人能提供一些关于组织本地测试框架的最佳实践的参考资料,我将非常感激。 提前谢谢
提前谢谢你 如果看不到更多的代码,就很难说为什么测试配置不能满足您的需要。我创建了一个示例项目,其中有几个不同的示例与您的案例和其他一些类似,您可以找到它。下面是您可以在那里找到的示例列表,并附有简短的说明
这个测试失败了,因为没有提供任何配置(除了控制器bean),没有找到和注入bean
这里我们使用一个测试配置为控制器提供一个服务bean,但我们仍然缺少该服务所使用的bean-
mockedutilitinterface
(见下文)
这个测试通过了-TestConfiguration提供了服务bean,我们还创建了一个
mockedutiInterface
bean作为模拟。模拟被注入到测试配置指向的服务中,服务被注入到实际控制器中
这个测试也通过了,并且它使用了所有的实际配置,没有加载任何测试配置,所以所有的bean都是来自实际应用程序的bean(就像它在容器中运行一样)
下面是一个内部类配置的示例,它覆盖一些bean,使用一些实际的bean,还可以使用提供的
@MockBeans
您可以找到很多方法来为测试配置bean。如果看不到更多的代码,很难说为什么测试配置不能满足您的需要。我创建了一个示例项目,其中有几个不同的示例与您的案例和其他一些类似,您可以找到它。下面是您可以在那里找到的示例列表,并附有简短的说明
这个测试失败了,因为没有提供任何配置(除了控制器bean),没有找到和注入bean
这里我们使用一个测试配置为控制器提供一个服务bean,但我们仍然缺少该服务所使用的bean-
mockedutilitinterface
(见下文)
这个测试通过了-TestConfiguration提供了服务bean,我们还创建了一个
mockedutiInterface
bean作为模拟。模拟被注入到测试配置指向的服务中,服务被注入到实际控制器中
这个测试也通过了,并且它使用了所有的实际配置,没有加载任何测试配置,所以所有的bean都是来自实际应用程序的bean(就像它在容器中运行一样)
下面是一个内部类配置的示例,它覆盖一些bean,使用一些实际的bean,还可以使用提供的
@MockBeans
您可以找到很多方法来为测试配置bean。放弃您的测试配置,因为这会破坏用于测试的spring boot应用程序的正确初始化。如果您的目标是只测试一个控制器,请使用
@WebMvcTest
而不是全面的@springbootest
。我从这种方法开始,测试尝试加载完整的上下文,这是@springbootest
的全部要点,如果这不是您想要的,请不要使用@springbootest
。如果目标是避免设置在控制器测试的Spring上下文中,您可能希望使用MockMvcBuilders
并准备一个独立的MockMvc
。放弃您的测试配置,因为这会中断Spring引导应用程序的正确初始化以进行测试。如果您的目标是只测试一个控制器,请使用@WebMvcTest
而不是全面的@springbootest
。我从这种方法开始,测试尝试加载完整的上下文,这是@springbootest
的全部要点,如果这不是您想要的,请不要使用@springbootest
。如果目标是避免设置对于控制器测试,您可能希望使用MockMvcBuilders
并准备一个独立的MockMvc
。
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = ScoreTestConfiguration.class)
@AutoConfigureMockMvc
public class ScoreControllerTest {
private static final String SCORE_ENDPOINT = "/scoring";
private static final ObjectMapper MAPPER = new ObjectMapper();
@Autowired
private MockMvc mockMvc;
@MockBean
private ScoreService mockScoreService;
@BeforeEach
public void setUp() {
ScoreResponseDto response = getScoreResponseDto();
when(mockScoreService.getScore(any(),any(),any(),any(),any(),any())).thenReturn(response);
}
@NotNull
private ScoreResponseDto getScoreResponseDto() {
ScoreResponseDto response = new ScoreResponseDto();
response.setCid("qwe13423qw");
response.setData(new ScoreResponseDto.ScoringData(0.78f));
return response;
}
@Test
public void sendValidRequest_getResponse_andOkStatus() throws Exception {
String request = SCORE_ENDPOINT +
"/380715346789" + //msisdn
"?score_formula=1234" +
"&clientId=5678" +
"&cid=543" +
"&stateType=" + StateType.ACTIVE.name() +
"&subscriberType=" + SubscriberType.POSTPAID.getValue();
String jsonResponse = MAPPER.writeValueAsString(getScoreResponseDto());
System.out.println("jsonResponse: " + jsonResponse);
mockMvc.perform(get(request))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(jsonResponse));
}
@Configuration
@ComponentScan(basePackages = "com.some.path.to.rest")
class ScoreTestConfiguration {
}
@SpringBootTest(classes = TestedController.class)
@AutoConfigureMockMvc
public class TestWithoutConfiguration {
@Test
void fails() {
}
}
@SpringBootTest(classes = {TestedController.class, TestConfiguration.class})
@AutoConfigureMockMvc
public class TestWithTestButWithoutMockBeanConfiguration {
@Test
void fails() {
}
}
@SpringBootTest(classes = {TestedController.class, TestConfiguration.class})
@AutoConfigureMockMvc
public class TestWithTestConfiguration {
@Autowired
private MockMvc mockMvc;
@MockBean
private MockedUtilInterface mockedUtil;
@Test
void test() throws Exception {
when(mockedUtil.value())
.thenReturn(100);
mockMvc.perform(get("/test"))
.andExpect(content().string("100 - rest service from component scan value"));
}
}
@SpringBootTest
@AutoConfigureMockMvc
public class TestWithActualConfiguration {
@Autowired
private MockMvc mockMvc;
@Test
void test() throws Exception {
mockMvc.perform(get("/test"))
.andExpect(content().string("0 - not a test value"));
}
}
@SpringBootTest
@AutoConfigureMockMvc
public class TestWithInnerTestConfiguration {
@Configuration
@Import(TestedApplication.class)
static class InnerConfiguration {
@Bean
TestedServiceInterface service(MockedUtilInterface mockedUtil) {
return () -> mockedUtil.value() + " - inner value";
}
}
@Autowired
private MockMvc mockMvc;
@MockBean
private MockedUtilInterface mockedUtil;
@Test
void test() throws Exception {
when(mockedUtil.value())
.thenReturn(12345);
mockMvc.perform(get("/test"))
.andExpect(content().string("12345 - inner value"));
}
}