如何测试Spring MVC配置是否存在?

时间:2014-05-09 14:30:42

标签: spring spring-mvc tdd

我希望有一个测试,当Spring MVC配置不存在时会失败,并且当它存在时会通过。例如,我有一个以下测试,以确保我配置了Spring Security(此测试基于春季的类似测试):


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SecurityConfiguration.class, JpaConfiguration.class })
@WebAppConfiguration
public class SecurityConfigurationTest {

    @Autowired
    private org.springframework.security.web.FilterChainProxy springSecurityFilterChain;

    @Autowired
    private AuthenticationManagerBuilder auth;

    @Test
    public void shouldLoadSecurityConfiguration() {}

    @Test
    public void shouldUseCustomUserDetailsService() {
        assertNotNull(auth.getDefaultUserDetailsService());
        assertTrue(auth.getDefaultUserDetailsService() instanceof UserDetailsServiceImpl);
    }

}

基本上它只测试Spring Security配置的存在,这迫使我在遵守TDD时创建这样的配置。我尝试根据this Spring MVC test创建一个类似的测试:


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SecurityConfiguration.class, JpaConfiguration.class})
@WebAppConfiguration
public class WebConfigurationTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void shouldReturn404ForNonExistingPage() throws Exception {
        this.mockMvc.perform(get("/mandible-is-inorganic")).andExpect(status().isNotFound());
    }

}

但是,通过所需的所有测试都是下载spring-webmvc依赖项。它设法通过,而没有我扩展AbstractAnnotationConfigDispatcherServletInitializer和没有@ EnableWebMvc注释的Web配置。我有什么选择?

0 个答案:

没有答案