使用JUnit和Mockito测试spring mvc控制器 得到这条线
WARNING: No mapping found for HTTP request with URI [application] in DispatcherServlet with name ''
这是否意味着未加载DispatcherServlet?如何正确配置?感谢
测试痕迹
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application],methods=[GET]}" onto public java.lang.String com.resman.web.controller.ApplicationController.index(org.springframework.ui.Model,com.resman.service.user.DBAuthenticationService$CustomUser)
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application/remove-app/{id}],methods=[GET]}" onto public java.lang.String com.resman.web.controller.ApplicationController.remove(java.lang.Long,com.resman.service.user.DBAuthenticationService$CustomUser)
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application/profile-app/{id}],methods=[GET]}" onto public java.lang.String com.resman.web.controller.ApplicationController.findProfile(org.springframework.ui.Model,java.lang.Long,com.resman.service.user.DBAuthenticationService$CustomUser)
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application/create-app-form],methods=[GET]}" onto public java.lang.String com.resman.web.controller.ApplicationController.createApplicationForm(org.springframework.ui.Model)
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application/create-app],methods=[POST]}" onto public java.lang.String com.resman.web.controller.ApplicationController.createApplication(org.springframework.ui.Model,java.util.Date,java.util.Date,java.lang.Long,java.lang.String,com.resman.service.user.DBAuthenticationService$CustomUser) throws java.text.ParseException
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application/edit-app],methods=[POST]}" onto public java.lang.String com.resman.web.controller.ApplicationController.editApplication(org.springframework.ui.Model,java.lang.Long,int,java.util.Date,java.lang.String,java.lang.Long,com.resman.service.user.DBAuthenticationService$CustomUser)
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application/all-apps],methods=[GET]}" onto public java.lang.String com.resman.web.controller.ApplicationController.findAll(org.springframework.ui.Model,com.resman.service.user.DBAuthenticationService$CustomUser)
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application/find-for-approver],methods=[GET]}" onto public java.lang.String com.resman.web.controller.ApplicationController.findForApprover(org.springframework.ui.Model)
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register
INFO: Mapped "{[/application/find-for-admin],methods=[GET]}" onto public java.lang.String com.resman.web.controller.ApplicationController.findForAdmin(org.springframework.ui.Model)
Jan 09, 2017 10:03:12 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: org.springframework.test.web.servlet.setup.StubWebApplicationContext@34e9fd99
Jan 09, 2017 10:03:12 PM org.springframework.mock.web.MockServletContext log
INFO: Initializing Spring FrameworkServlet ''
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.TestDispatcherServlet initServletBean
INFO: FrameworkServlet '': initialization started
Jan 09, 2017 10:03:12 PM org.springframework.test.web.servlet.TestDispatcherServlet initServletBean
INFO: FrameworkServlet '': initialization completed in 2 ms
Jan 09, 2017 10:03:13 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [application] in DispatcherServlet with name ''
java.lang.AssertionError: Status
Expected :200
Actual :404
<Click to see difference>
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$10.match(StatusResultMatchers.java:653)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:152)
at com.resman.web.controller.ApplicationControllerTest.indexText(ApplicationControllerTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code 255
类本身
@Controller
public class ApplicationController {
private static final Logger LOGGER =
LogManager.getLogger(ApplicationController.class);
@Autowired
private ApplicationDAO applicationDAO;
@Autowired
private UserDao userDAO;
@Autowired
private ResourceDao resourceDao;
@Autowired
private ResourceTypeService resourceTypeService;
@Autowired
private ApplicationValidator applicationValidator;
@RequestMapping(value = "application", method = RequestMethod.GET)
public String index(Model model, @AuthenticationPrincipal DBAuthenticationService.CustomUser currentUser) {
LOGGER.debug("application index page");
model.addAttribute("userRole", currentUser.getUserRole());
return "application/index";
}
测试类
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@WebAppConfiguration
public class ApplicationControllerTest {
@Mock
private ApplicationDAO applicationDAO;
@Mock
private UserDao userDAO;
@Mock
private ResourceDao resourceDao;
@Mock
private ResourceTypeService resourceTypeService;
@Mock
private ApplicationValidator applicationValidator;
@InjectMocks
private ApplicationController controller;
private MockMvc mockMvc;
@Before
public void setUp(){
this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void indexText() throws Exception {
mockMvc.perform(get("application")).andExpect(status().isOk());
}
}
配置类
@Configuration
@EnableWebMvc
@ComponentScan("com.resman.*")
@Import({ SecurityConfig.class })
@EnableTransactionManagement
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/view/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@//localhost:1521/XE");
dataSource.setUsername("project");
dataSource.setPassword("projectsql");
return dataSource;
}
@Bean
public DataSourceTransactionManager dataSourceTransactionsManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
的web.xml
<web-app id="resourceManager" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>
答案 0 :(得分:0)
可能你可能需要 默认-servlet的处理程序强> 在你的AppConfig中,
例如:
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
答案 1 :(得分:0)
问题在于
mockMvc.perform(get("application")).andExpect(status().isOk());
"application"
应更改为"/application"
,一切正常