问题描述
我正在尝试测试Spring Web MVC控制器。
我有以下测试课:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { \"classpath:controller-test.xml\" })
public class EmployeesControllerMockTest implements ApplicationContextAware {
// this servlet is going to be instantiated by ourselves
// so that we can test the servlet behavIoUr w/o actual web container
// deployment
protected dispatcherServlet servlet;
// we need to get at the context already loaded via the @ContextConfiguration annotation.
protected ApplicationContext appCtx;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
appCtx = applicationContext;
}
/**
* using @Before is convenient,but with JUnit these annotated methods must be public (not like e.g. testNG)
* so be aware that no \"secrets\" are being set/got in these init-style methods ;_)! !
*/
@Before
public void initdispatcherServlet() {
servlet = new dispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(
WebApplicationContext parent) throws BeansException {
GenericWebApplicationContext gwac = new GenericWebApplicationContext();
gwac.setParent(appCtx);
gwac.refresh();
return gwac;
}
};
}
@Test
public void test() {
MockHttpServletRequest request = new MockHttpServletRequest(\"GET\",\"/employees/test\");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
servlet.service(request,response);
} catch (Exception e) {
e.printstacktrace();
}
System.out.println(response.getStatus());
}
}
并诠释了controller-test.xml我有:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<beans xmlns=\"http://www.springframework.org/schema/beans\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:p=\"http://www.springframework.org/schema/p\"
xmlns:context=\"http://www.springframework.org/schema/context\"
xmlns:mvc=\"http://www.springframework.org/schema/mvc\" xmlns:oxm=\"http://www.springframework.org/schema/oxm\"
xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd\">
<!-- Force the Spring container to search the controller package for all stereotype components. Detect all
components marked with the @Controller annotations in the scan. -->
<context:component-scan base-package=\"controller\" />
<!-- Enable automatic annotation-driven declaration. Configure the @Controller programming model -->
<mvc:annotation-driven/>
</beans>
但是我在servlet.service()调用上遇到以下异常:
java.lang.NullPointerException
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:682)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at tests.controller.EmployeesControllerMockTest.test(EmployeesControllerMockTest.java:68)
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:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runchild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
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.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
at $Proxy0.invoke(UnkNown Source)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
难道我做错了什么?
解决方法
您无需花大力气在测试中配置servlet-Spring MVC控制器可直接进行单元测试。控制器不依赖servlet,因此servlet对您的单元测试是多余的。
只需在测试中创建控制器对象,然后根据需要将请求/响应对象传递给它。
如果要测试servlet,控制器和配置之间的交互,则应在servlet容器内进行此测试,作为功能测试。