模拟Spring MessageContext

时间:2014-01-16 09:32:49

标签: java spring unit-testing junit mockito

我想模仿这些方法:

    SaajSoapMessage saajSoapMessage = (SaajSoapMessage) messageContext.getRequest();
    SoapBody requestBody = saajSoapMessage.getSoapBody();

我用这个

尝试了
messageContextMock = mock(MessageContext.class);
saajSoapMessageMock = mock(SaajSoapMessage.class);

when(messageContextMock.getRequest()).thenReturn((SaajSoapMessage) saajSoapMessageMock);
when(saajSoapMessageMock.getSoapBody()).thenReturn(soapBodyMock);

但我在模拟getSoapBody()时遇到问题,因为Junit写信给我:

at org.springframework.ws.soap.AbstractSoapMessage.getSoapBody(AbstractSoapMessage.java:36)

如何正确模拟此操作?

编辑(完整筹码):

显示java.lang.NullPointerException     在org.springframework.ws.soap.AbstractSoapMessage.getSoapBody(AbstractSoapMessage.java:36)     在com.project.my.WebMessageTest.setUp(WebMessageTest.java:33)     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)     在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:45)     在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)     在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)     在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:231)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)     在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)     在org.junit.runners.ParentRunner.run(ParentRunner.java:300)     在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)     在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:2)

尝试向模拟对象添加正文,试试这个:

messageContextMock = mock(MessageContext.class);

saajSoapMessageMock = mock(SaajSoapMessage.class);
soapEnvelopeMock = mock(SOAPEnvelope.class);

when(saajSoapMessageMock.getEnvelope()).thenReturn(soapEnvelopeMock);
when(messageContextMock.getRequest()).thenReturn(saajSoapMessageMock);