2016-03-19 22 views
0

私はMockitoを使っていくつかのテストを書いています。私は、基本的にAsyncTaskを介して非同期呼び出しを同期呼び出しするテストしたいクラスがあります。 (編集:フルスタックトレース):それは私のテストを実行し、このエラーを与えるMockitoとAsyncTasks

public class GetEntryTest { 
    private GetEntryImpl mGetEntry; 
    @Mock 
    private DataEntryRepository mRepository; 
    @Captor 
    private ArgumentCaptor<Callback> mCallbackArgumentCaptor; 
    private DataEntry mDataEntry; 

    @Before 
    public void setup() throws Exception { 
     MockitoAnnotations.initMocks(this); 
     mDataEntry = getFakeEntry(); 
     when(mRepository.getEntry(anyLong())).thenReturn(mDataEntry); 
     mGetEntry = new GetEntryImpl(mRepository); 
    } 

    @SuppressWarnings("unchecked") 
    @Test 
    public void testGetEntry_success() throws Exception { 
     mGetEntry.execute(1L, mCallbackArgumentCaptor.capture()); 
     verify(mRepository).getEntry(eq(1L)); 
     Callback<DataEntry> callback = mCallbackArgumentCaptor.getValue(); 
     verify(callback).onResult(eq(mDataEntry)); 
    } 
} 

この

はテストのクラスです:

public class GetEntryImpl implements GetEntry { 

    private final DataEntryRepository mDataEntryRepository; 

    public GetEntryImpl() { 
     this(new DataEntryRepositoryImpl()); 
    } 

    public GetEntryImpl(@NonNull final DataEntryRepository repository) { 
     mDataEntryRepository = repository; 
    } 

    @Override 
    public void execute(final long id, final Callback<DataEntry> callback) { 
     AsyncTask.execute(new Runnable() { 
      @Override 
      public void run() { 
       DataEntry dataEntry = mDataEntryRepository.getEntry(id); 
       if (dataEntry != null) { 
        callback.onResult(dataEntry); 
       } else { 
        callback.onError("TODO", null); 
       } 
      } 
     }); 
    } 
} 

をそして、これは私のテストです

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced argument matcher detected here: 

-> at com.xyz.interactor.GetEntryTest.testGetEntry_success(GetEntryTest.java:45) 

You cannot use argument matchers outside of verification or stubbing. 
Examples of correct usage of argument matchers: 
    when(mock.get(anyInt())).thenReturn(null); 
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); 
    verify(mock).someMethod(contains("foo")) 

Also, this error might show up because you use argument matchers with methods that cannot be mocked. 
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode(). 
Mocking methods declared on non-public parent classes is not supported. 


    at com.xyz.interactor.GetEntryTest.testGetEntry_success(GetEntryTest.java:46) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251) 
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188) 
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 


Exception: java.lang.NullPointerException thrown from the UncaughtExceptionHandler in thread "AsyncTask #1" 

Process finished with exit code 255 

ライン45がテストの最初の行です。

mGetEntry.execute(1L, mCallbackArgumentCaptor.capture()); 
+0

mockitoに問題が起こっているようなスタックトレースからもう少し提供していただけますか? –

+0

フルスタックトレースを追加。 – Francesc

+0

'DataEntryRepository'クラスの' getEntry() 'メソッドの署名は何ですか? –

答えて

2

CallbackにはArgumentCaptorは必要ありませんが、モックはありません。

mCallbackがある
@SuppressWarnings("unchecked") 
@Test 
public void testGetEntry_success() throws Exception { 
    mGetEntry.execute(1L, mCallback); 
    verify(mRepository).getEntry(eq(1L)); 
    verify(mCallback).onResult(eq(mDataEntry)); 
} 

:あなたはverify文の外でArgumentCaptorを使用しているため

@Mock 
private Callback<DataEntry> mCallback; 

mockitoがエラーを出力します。 ArgumentCaptorの使用例hereが表示されます。

+0

素晴らしいです、ありがとう!それはあなたがそれを見たときにとても分かります... – Francesc