2012-09-20 62 views
1

我想是這樣的,但仍然拋出NullPointerException異常如何用嘲弄的Mockito ServletActionContext.getRequest(),我們使用的是Struts2的

@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)); 
} 

張貼堆棧跟蹤:

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 
+0

請問您可以發佈一些更多的代碼? –

+0

你如何創建你的動作對象(SUT)? –

+0

這裏@injectMocks私人MyAction行動本身爲我創建對象,所以我打電話給我這樣的真實方法:action.mysaveMethod(); –

回答

0

HM,我會說你還需要創建動作對象,如

@InjectMocks private MyAction action = new MyAction(); 

請參閱http://docs.mockito.googlecode.com/hg/org/mockito/InjectMocks.html對於某些e xamples

+0

,沒有幫助.... –

+0

請調試是什麼導致NPE –

+0

http://stackoverflow.com/questions/10756591/strut-2- 3-1-2-單元測試-如何去除的 - 彈簧 - 相互依賴-VS-NPE-與-getcontex –