简体   繁体   English

Spring Junit测试类中的多种配置

[英]Multiple Configurations In Spring Junit Test Class

We are using SpringJUnit4ClassRunner with ContextConfiguration annotation, for example, testing a front end controller: 我们正在使用带有ContextConfiguration批注的SpringJUnit4ClassRunner ,例如,测试前端控制器:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class FrontEndControllerTest {

    private MockMvc mockMvc;

    @Autowired
    private FrontEndController frontEndController;

    @Before
    public void init() {
        mockMvc = MockMvcBuilders.standaloneSetup(frontEndController).build();
    }

    @Test
    public void shouldDirectToSomeView() throws Exception {
        mockMvc.perform(get("/"))
                .andExpect(status().isOk())
                .andExpect(view().name("someView"));
    }

    @Configuration
    public static class FrontEndControllerTestConfiguration {

        @Bean
        public FrontEndController defaultController() {
            return // init controller
        }

        @Bean
        public SomeConfigObject config() {
            return // some config
        }
    }
}

Now, I want to add another test with a different configuration into the same the class. 现在,我想在同一个类中添加另一个具有不同配置的测试。 For example, inject a mock with a failing database operation, or or or. 例如,使用失败的数据库操作或或或注入模拟。

Is it possible add another test like this (I know, will not compile): 是否可以添加另一个这样的测试(我知道,将无法编译):

@Test
@ContextConfiguration(/* My other configuration */)
public void shouldDoSomeOtherStuff_InvalidConfiguration() {
      // ...
} 

In Spring 4 it is not possible because ContextConfiguration can only be declared as annotation in front of the class. 在Spring 4中这是不可能的,因为ContextConfiguration只能在类前面声明为注释。 Is there something like this in Spring 4? 春季4是否有类似的事情? Do you recommend to try another approach? 您是否建议尝试其他方法?

I found a solution: The problem is that I want to get new mocks for every test and I want to modify them before I perform mockMvc.perform(get("/")) . 我找到了一个解决方案:问题是我想为每个测试获取新的mockMvc.perform(get("/"))并且想要在执行mockMvc.perform(get("/"))之前对其进行修改。 Therefore you can use @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) annotation. 因此,您可以使用@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)批注。 Here is my code: 这是我的代码:

@ContextConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@RunWith(SpringJUnit4ClassRunner.class)
public class FrontEndControllerTest {

    private MockMvc mockMvc;

    @Autowired
    private FrontEndController frontEndController;

    @Autowired
    private SomeConfigObject config;

    @Before
    public void init() {
        mockMvc = MockMvcBuilders.standaloneSetup(frontEndController).build();
    }

    @Test
    public void shouldDirectToSomeView() throws Exception {
        // mock valid behaviour of config

        mockMvc.perform(get("/"))
                .andExpect(status().isOk())
                .andExpect(view().name("someView"));
    }

    @Test
    public void shouldNotDirectToSomeView_InvalidConfiguration() throws Exception {
        // mock invalid behaviour of config

        mockMvc.perform(get("/"))
                .andExpect(status().is5xxServerError())
    }

    @Configuration
    public static class FrontEndControllerTestConfiguration {

        @Bean
        public FrontEndController defaultController() {
            return // init controller
        }

        @Bean
        public SomeConfigObject config() {
            return mock(SomeConfigObject.class);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM