我在用:
- 春天 3.1
- JUnit 4.10
我正在尝试为具有 RedirectAttributes 的控制器编写一个 junit 测试用例。
控制器的签名是:
@RequestMapping(method = RequestMethod.POST)
public String homePOST(@Validated({ IBasic.class }) @ModelAttribute("userCommand") User userCommand,
BindingResult result, Model model,
RedirectAttributes flashAttributes)
JUnit 测试用例是:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/root-context.xml", "/servlet-context.xml" })
public class HomeControllerTest {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
@Autowired
private RequestMappingHandlerMapping handlerMapping;
@Test
public void testHomePOST() {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
request.addParameter("username", "user1");
request.addParameter("password", "mypasswd");
MockHttpServletResponse response = new MockHttpServletResponse();
Object handler;
try {
handler = handlerMapping.getHandler(request).getHandler();
ModelAndView modelAndView = handlerAdapter.handle(request,
response, handler);
assertViewName(modelAndView, "redirect:/myview");
} catch (Exception e) {
String err = "Error executing controller : " + e.toString();
fail(err);
}
}
}
当 handlerAdapter.handle() 被执行时,我得到:
java.lang.AssertionError: Error executing controller : java.lang.NullPointerException
at org.junit.Assert.fail(Assert.java:93)
at com.myapps.service.impl.test.HomeControllerTest.testHomePOST(HomeControllerTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
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:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
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:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
如果我从控制器中删除 RedirectAttributes,它工作正常。
谁能提供一些帮助如何使用 RedirectAttributes 测试控制器?
先感谢您。
为了模拟这一点,这里是控制器代码:
@Controller
@RequestMapping(value = "/")
public class HomeController {
@RequestMapping(method = RequestMethod.POST)
public String homePOST(
@Validated({ IBasic.class }) @ModelAttribute("userCommand") User userCommand,
BindingResult result, Model model,
RedirectAttributes flashAttributes) {
return "redirect:/myview";
}
}