我試圖嘲弄一類像這樣(感謝的谷歌代碼的設計,很難在測試環境中使用):EasyMock的3類:
GoogleAuthorizationCodeGrant googleAuthorizationCodeGrant = EasyMock.createMock(GoogleAuthorizationCodeGrant.class);
然後我設立一個模擬的方法調用像這樣:
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
EasyMock.expect(googleAuthorizationCodeGrant.execute()).andReturn(accessTokenResponse);
然而,它噴出的例外,因爲googleAuthorizationCodeGrant.execute(中)它會嘗試在googleAuthorizationCodeGrant.execute運行的實際代碼()。我在這裏錯過了什麼嗎?我期望cglib代理包裝這個類,並覆蓋它的方法什麼也不做,因爲我不希望它們在模擬時執行。應該不需要保留任何業務邏輯,因爲我正在定義各種方法調用的行爲。
編輯
注 - 我認爲這裏存在一些混亂。拋出異常:
EasyMock.expect(googleAuthorizationCodeGrant.execute()).andReturn(accessTokenResponse)
不是當我把模擬放在重放模式,因爲我從來沒有真正達到過這個代碼。引發異常的原因是因爲GoogleAuthorizationCodeGrant的成員爲null,並且在.execute()上對其調用方法。如果我使用new運算符實例化GoogleAuthorizationCodeGrant,則該成員不會爲空。然而我不明白爲什麼EasyMock會像這樣在一個類上工作,因爲我期望它創建一個代理來封裝實現,從而無需執行任何操作。
這裏是一個堆棧跟蹤:
java.lang.NullPointerException
at com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.executeUnparsed(AccessTokenRequest.java:451)
at com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.execute(AccessTokenRequest.java:475)
at uk.co.domain.service.google.GmailContactPollerTest.testDoPoll(GmailContactPollerTest.java:64)
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: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.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(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
你能否將異常的堆棧跟蹤添加到問題中? – Alb 2012-02-29 16:24:28
您是否將模擬對象設置爲重播狀態? – 2012-03-09 04:31:42