我正在尝试测试我的一个名为Store的类,尤其是一个名为removeFromFavourite的方法 下面是方法
public void removeFromFavourite(Quote quote) {
if(quote == null) {
throw new IllegalArgumentException("Quote can't be null");
}
if (quote.getId() <1) {
throw new IllegalArgumentException("Quote id can't be less than 1");
}
realm.beginTransaction();
QuoteRealMObject object = realm.where(QuoteRealMObject.class).
equalTo(QuoteRealMObject.ID, quote.getId()).findFirst();
if(object != null) {
deleteObject(object);
}
realm.commitTransaction();
}
private void deleteObject(RealmObject object) {
object.deleteFromRealm();
}
我创建了deleteObject方法来调用deleteFromRealm,因为deleteFromRealm是最终的,因此无法对其进行模拟。
我的期望是通过创建间谍来模拟对商店对象的“ deleteObject”调用
这是我的@Before
方法
private Realm mockedRealm;
private Store store;
@Before
public void setUp() throws Exception {
mockedRealm = Mockito.mock(Realm.class);
store = new Store(mockedRealm);
store = PowerMockito.spy(store);
}
所以我创建了Realm的模拟对象,并用它来创建Store类的对象,然后从商店的对象中创建了一个间谍并将其分配回存储对象本身。
下面是removeFromFavourite的测试方法
@Test
public void removeFromFavourite() throws Exception {
Quote quot = new Quote();
quot.setId(1);
RealmQuery<QuoteRealMObject> query = Mockito.mock(RealmQuery.class);
RealmQuery<QuoteRealMObject> filteredQuery = Mockito.mock(RealmQuery.class);
Mockito.when(mockedRealm.where(QuoteRealMObject.class)).thenReturn(query);
Mockito.when(query.equalTo(QuoteRealMObject.ID, 1)).thenReturn(filteredQuery);
QuoteRealMObject mockedObject = Mockito.mock(QuoteRealMObject.class);
Mockito.when(filteredQuery.findFirst()).thenReturn(mockedObject);
PowerMockito.doNothing().when(store, "deleteObject", mockedObject);
store.removeFromFavourite(quot);
}
这里的行PowerMockito.doNothing().when(store, "deleteObject", mockedObject);
本身正在调用真实的私有方法“ deleteObject”
而且我收到未完成的模拟异常
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.kgcorner.vachan.io.StoreTest.removeFromFavourite(StoreTest.java:132)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
at org.mockito.internal.runners.DefaultInternalRunner$1$1.testFinished(DefaultInternalRunner.java:70)
at org.junit.runner.notification.SynchronizedRunListener.testFinished(SynchronizedRunListener.java:56)
at org.junit.runner.notification.RunNotifier$7.notifyListener(RunNotifier.java:190)
at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:72)
at org.junit.runner.notification.RunNotifier.fireTestFinished(RunNotifier.java:187)
at org.junit.internal.runners.model.EachTestNotifier.fireTestFinished(EachTestNotifier.java:38)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:331)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
任何想法,我在哪里误导?
答案 0 :(得分:0)
我认为when
中的第三个参数应该是模拟匹配器:
PowerMockito.doNothing().when(store, "deleteObject", eq(mockedObject));
答案 1 :(得分:0)
Mockito提供了一种方法 doNothing()来模拟对void方法的调用。对于上面的问题,可以像
那样模拟 deleteFromRealm 方法Mockito.doNothing().when(mockedObject).deleteFromRealm();
deleteFromRealm 是最终的,不能被嘲笑。模拟相同类的方法 deleteObject
ClassToBeTested testClass = new ClassToBeTested();
ClassToBeTested testClassSpy = PowerMockito.spy(testClass);
PowerMockito.doNothing().when(testClassSpy, "deleteObject", mockedRealm);