how to mock ServletActionContext.getRequest() using Mockito we are using Struts2

寵の児 提交于 2019-12-24 08:11:24

问题


I tried something like this but still throwing NullPointerException

@RunWith(MockitoJUnitRunner.class)
public class MyActionTest extends BaseActionTestCase {
      @Mock private ServletActionContext context;  

@Mock
private serviceclass serviceMock;
@InjectMocks
private MyAction action;

@Test
public void testSave() throws Exception {
      when(serviceMock.getTaskCode()).thenReturn("tc");
    action.save();
    verify(serviceMock).saveObject(any(Task.class));
}

posting stacktrace:

java.lang.NullPointerException
    at org.apache.struts2.ServletActionContext.getRequest(ServletActionContext.java:112)
    at com.princess.ilo.webapp.action.BaseAction.getRequest(BaseAction.java:176)
    at com.princess.ilo.webapp.action.BaseAction.getSession(BaseAction.java:198)
    at com.princess.ilo.webapp.action.BaseAction.getUser(BaseAction.java:206)
    at com.princess.ilo.webapp.action.TaskAction.save(TaskAction.java:116)
    at com.princess.ilo.webapp.action.TaskActionTest.testSave(TaskActionTest.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.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.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    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.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    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

回答1:


hm, I would say you still need to create the action object, like

    @InjectMocks private MyAction action = new MyAction();

see http://docs.mockito.googlecode.com/hg/org/mockito/InjectMocks.html for some examples



来源:https://stackoverflow.com/questions/12517374/how-to-mock-servletactioncontext-getrequest-using-mockito-we-are-using-struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!