本文介绍了@DirtiesContext不适用于@Nested测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过数小时的Google研究,我仍然不知道如何将 @DirtiesContext @Nested 类一起使用.假定下面的集成测试类:

  @ExtendWith(SpringExtension.class)@SpringBootTest@AutoConfigureMockMvc(addFilters =假)公共类StuffIntegrationTests {@Autowired私有的StuffRepository stuffRepository;@Autowired私有WebApplicationContext上下文;私人MockMvc mvc;//...@BeforeEach私人无效setUp(){mvc = MockMvcBuilders.webAppContextSetup(上下文).apply(springSecurity()).建造();//...}@DisplayName("POST-/stuffs")@嵌套类saveStuff {@DisplayName(返回2xx")@嵌套Return2xx类{//一些测试方法@DisplayName(返回4xx")@嵌套Return4xx类{//一些测试方法}@DisplayName("GET-/stuffs/{stuffId}")@嵌套class findStuffById {@DisplayName(返回2xx")@嵌套Return2xx类{//一些测试方法@DisplayName(返回4xx")@嵌套Return4xx类{//一些测试方法}} 

如您所见,我想通过按Enpoints将其拆分为嵌套类来使该类更具可读性,并且对于每个Endpoint,都有按Http响应状态拆分的嵌套类.

我尝试在多个级别上使用 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS),它不会在每个类之前清除Spring Context,如果我将 @DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD).

我的目标是在类 SaveStuff 的每个方法之前清除Spring Context,并在类 findStuffById

的开头仅清洁一次上下文.

非常感谢您的帮助.

解决方案

直到Spring Framework 5.3之前,Spring都不支持从JUnit Jupiter @Nested 测试类的封闭类继承测试配置./p>

有关详细信息,请参见 @Nested 测试类配置部分.

After hours of research on Google I still can't figure out how to use @DirtiesContext with @Nestedclasses. Assuming the following Integration Test class :

@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
public class StuffIntegrationTests {

    @Autowired
    private StuffRepository stuffRepository;

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;

    // ...

    @BeforeEach
       private void setUp() {
       mvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();
       // ...
       }

    @DisplayName("POST - /stuffs")
    @Nested
    class saveStuff{

        @DisplayName("Return 2xx")
        @Nested
        class Return2xx{

           // some test methods

        @DisplayName("Return 4xx")
        @Nested
        class Return4xx{

          // some tests methods

    }

    @DisplayName("GET - /stuffs/{stuffId}")
    @Nested
    class findStuffById{

        @DisplayName("Return 2xx")
        @Nested
        class Return2xx{

           // some test methods

        @DisplayName("Return 4xx")
        @Nested
        class Return4xx{

          // some tests methods

    }
}

As you can see I want to make the class more readable by spliting it in nested classes by Enpoints and for each Endpoint there are nested classes to split by Http response status.

I tried to use @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS) at multiple level and it doesn't clean the Spring Context before each class, The result is the same if I put @DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD) on each methods.

My goals are to clean the Spring Context before each methods of class SaveStuff and clean the context only once at the begin of the class findStuffById

Thanks a lot for your help.

解决方案

Spring did not provide support for inheriting test configuration from enclosing classes for a JUnit Jupiter @Nested test class until Spring Framework 5.3.

For details, see the @Nested test class configuration section of the Spring reference manual.

这篇关于@DirtiesContext不适用于@Nested测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:38