我正在尝试使用 Mockito 测试一个 servlet,并看到以下异常。不知道为什么 response.getStatus()
方法会返回 0 而不是 200,甚至response.getContentType()
也返回 null。
例外
Running com.study.mockito.controllers.WelcomeControllerTest
Into the WelcomeController...
response.getStatus() = 0
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.27 sec <<< FAILURE!
testProcessRequest(com.study.mockito.controllers.WelcomeControllerTest) Time elapsed: 0.23 sec <<< FAILURE!
java.lang.AssertionError: expected:<text/html> but was:<null>
at org.junit.Assert.fail(Assert.java:93)
at org.junit.Assert.failNotEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:128)
at org.junit.Assert.assertEquals(Assert.java:147)
at com.study.mockito.controllers.WelcomeControllerTest.testProcessRequest(WelcomeControllerTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
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.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Results :
Failed tests: testProcessRequest(com.study.mockito.controllers.WelcomeControllerTest): expected:<text/html> but was:<null>
欢迎控制器测试.java
public class WelcomeControllerTest {
private HttpServletRequest request;
private HttpServletResponse response;
private WelcomeController controller;
private RequestDispatcher rd;
private ServletContext appContext;
@Before
public void setUp() {
controller = new WelcomeController();
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
rd = mock(RequestDispatcher.class);
appContext = mock(ServletContext.class);
}
@Test
public void testProcessRequest() throws ServletException, IOException {
when(request.getServletContext()).thenReturn(appContext);
when(appContext.getRequestDispatcher(anyString())).thenReturn(rd);
controller.doGet(request, response);
verify(rd).forward(request, response);
System.out.println("response.getStatus() = " + response.getStatus());
assertEquals("text/html", response.getContentType());
}
}
欢迎控制器.java
@WebServlet(name = "WelcomeController", urlPatterns = {"/welcome.htm"})
public class WelcomeController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Into the WelcomeController...");
RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/jsp/welcome.jsp");
rd.forward(request, response);
}
}
不知道为什么 response.getStatus() 方法会返回 0 而不是 200,>甚至 response.getContentType() 也返回 null。
因为response
是一个模拟,所以当你response
调用getStatus()
、getContentType()
或任何其他方法时,你会得到:
-
编程行为(如果您使用
Mockito.when(...
或Mockito.doXXX(
编程某些行为 - 响应类型的默认值(
int
=>0
、String
=>null
等)
在您的情况下,response.getStatus()
返回一个int
并且没有编程的行为,因此您得到0
,response.getContentType()
返回一个String
并且没有编程的行为,因此您会得到null
项目主页上有一些值得一读的文档。另请参阅常见问题