2016-02-14 45 views
2

我有一個彈簧單元測試,它顯示成功,即使在Web瀏覽器中發出相同的請求時,它也會返回404,因爲找不到視圖。如何在彈簧無法解析視圖時進行單元測試

這是一個故意測試,因爲如果控制器正在返回一個不存在的視圖,我想在單元測試中發現錯誤。

但是在單元測試中,我已經設置爲尋找404,但http響應代碼是200.我如何調整我的應用程序或測試用例以捕獲從不存在的控制器返回視圖的錯誤。

控制器

package com.doyleisgod.springmavenexample.controllers; 


import org.apache.log4j.Logger; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 

@Controller 
public class HomeController { 
    Logger log = Logger.getLogger(this.getClass()); 

    @RequestMapping("/testings.htm") 
    public String Index (Model model){ 
     log.debug("creating the testing page"); 
     model.addAttribute("name", "Chris"); 
     return "something"; 
    } 
} 

測試類

package com.doyleisgod.springmavenexample.controllers.tests; 

import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.web.WebAppConfiguration; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 
import org.springframework.web.context.WebApplicationContext; 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:testContext.xml", "classpath:smeApplicationContext-web.xml"}) 
@WebAppConfiguration 
public class HomeControllerTest { 
     private MockMvc mockMvc; 

     @Autowired 
     private WebApplicationContext webApplicationContext; 

     @Before 
     public void setUp() { 
      //We have to reset our mock between tests because the mock objects 
      //are managed by the Spring container. If we would not reset them, 
      //stubbing and verified behavior would "leak" from one test to another. 
      mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 
     } 

     @Test 
     public void indexTest() throws Exception { 


      mockMvc.perform(get("/testings.htm", 1L)) 
        .andExpect(status().isNotFound()); 
     } 
} 

堆棧跟蹤

java.lang.AssertionError: Status expected:<404> but was:<200> 
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60) 
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89) 
    at org.springframework.test.web.servlet.result.StatusResultMatchers$5.match(StatusResultMatchers.java:556) 
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:149) 
    at com.doyleisgod.springmavenexample.controllers.tests.HomeControllerTest.indexTest(HomeControllerTest.java:38) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

控制檯登錄

6-02-14 20:29:24 DEBUG TestDispatcherServlet:558 - Published WebApplicationContext of servlet '' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.] 
2016-02-14 20:29:24 INFO TestDispatcherServlet:498 - FrameworkServlet '': initialization completed in 21 ms 
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:139 - Servlet '' configured successfully 
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:843 - DispatcherServlet with name '' processing GET request for [/testings.htm] 
2016-02-14 20:29:24 DEBUG RequestMappingHandlerMapping:222 - Looking up handler method for path /testings.htm 
2016-02-14 20:29:24 DEBUG RequestMappingHandlerMapping:232 - Did not find handler method for [/testings.htm] 
2016-02-14 20:29:24 DEBUG SimpleUrlHandlerMapping:169 - Matching patterns for request [/testings.htm] are [/**] 
2016-02-14 20:29:24 DEBUG SimpleUrlHandlerMapping:194 - URI Template variables for request [/testings.htm] are {} 
2016-02-14 20:29:24 DEBUG SimpleUrlHandlerMapping:124 - Mapping [/testings.htm] to HandlerExecutionChain with handler [org.spring[email protected]18d68ff] and 1 interceptor 
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:932 - Last-Modified value for [/testings.htm] is: -1 
2016-02-14 20:29:24 DEBUG MockRequestDispatcher:67 - MockRequestDispatcher: forwarding to [default] 
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:1019 - Null ModelAndView returned to DispatcherServlet with name '': assuming HandlerAdapter completed request handling 
2016-02-14 20:29:24 DEBUG TestDispatcherServlet:991 - Successfully completed request 
2016-02-14 20:29:24 DEBUG DirtiesContextTestExecutionListener:94 - After test method: context [[email protected] testClass = HomeControllerTest, testInstance = com.doy[email protected]b107fd, testMethod = [email protected], testException = java.lang.AssertionError: Status expected:<404> but was:<200>, mergedContextConfiguration = [[email protected] testClass = HomeControllerTest, locations = '{classpath:testContext.xml, classpath:smeApplicationContext-web.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [false]. 
2016-02-14 20:29:24 DEBUG ServletTestExecutionListener:134 - Resetting RequestContextHolder for test context [[email protected] testClass = HomeControllerTest, testInstance = com.doy[email protected]b107fd, testMethod = [email protected], testException = java.lang.AssertionError: Status expected:<404> but was:<200>, mergedContextConfiguration = [[email protected] testClass = HomeControllerTest, locations = '{classpath:testContext.xml, classpath:smeApplicationContext-web.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]]. 
2016-02-14 20:29:24 DEBUG DirtiesContextTestExecutionListener:126 - After test class: context [[email protected] testClass = HomeControllerTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [[email protected] testClass = HomeControllerTest, locations = '{classpath:testContext.xml, classpath:smeApplicationContext-web.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], dirtiesContext [false]. 
2016-02-14 20:29:24 INFO GenericWebApplicationContext:873 - Closing org.s[email protected]124409e: startup date [Sun Feb 14 20:29:23 GMT 2016]; root of context hierarchy 
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:249 - Returning cached instance of singleton bean 'lifecycleProcessor' 
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:474 - Destroying singletons in org.s[email protected]19fcf7f: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,mvcUriComponentsContributor,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,viewResolver,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy 
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)#18': [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0] 
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)#9': [mvcUriComponentsContributor] 
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)#10': [(inner bean)#9] 
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)#1': [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0] 
2016-02-14 20:29:24 DEBUG DefaultListableBeanFactory:530 - Retrieved dependent beans for bean '(inner bean)': [org.springframework.web.servlet.handler.MappedInterceptor#0] 
+0

添加你的'/ testings.htm'處理程序.. –

+0

我已經添加了控制器,其中url處理程序被註釋爲映射。當我返回存在的視圖時,這工作正常,當我返回一個不存在的視圖時,我在Web瀏覽器中獲得404。但是在測試單元中,即使沒有找到視圖,也會返回http 200 –

回答

0

你是不是測試你認爲你正在測試:

@RequestMapping("/testings.htm") 
public String Index (Model model){ 
    log.debug("creating the testing page"); 
    model.addAttribute("name", "Chris"); 
    return "something"; 
} 

,你使用這個測試:

mockMvc.perform(get("/testings.htm", 1L)) 
     .andExpect(status().isNotFound()); 

您正在使用完全相同的映射(/testings.htm)所以它是找到。什麼意思,是:一個控制器被發現這個映射,並返回了一些東西=>Status.isFound()

您的測試將是有效用下面的URI:

mockMvc.perform(get("BAD_URI", 1L)) 
     .andExpect(status().isNotFound()); 

當您使用瀏覽器時,視圖被成功地從控制器檢索,但由於該視圖不存在,你結束了一個404

我想你想要做的是測試視圖返回

assertTrue(mockMvc.perform(MockMvcRequestBuilders.get("/testings.htms", 1L)) 
        .andReturn().getModelAndView() == null);