0
我在寫一些使用Mockito的測試。我有一個我想測試的類,基本上通過AsyncTask將同步調用變爲異步調用。Mockito和AsyncTasks
這是類測試:
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);
}
}
});
}
}
這是我的測試:
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));
}
}
它讓我在運行測試時,這個錯誤(編輯:完整的堆棧跟蹤):
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());
你可以從堆棧跟蹤中提供更多信息,比如mockito出了什麼問題? –
增加了完整的堆棧跟蹤。 – Francesc
DataEntryRepository類的getEntry()方法的簽名是什麼? –