java doNothing,当private void方法本身正在调用方法时

mitkmikd  于 2023-08-01  发布在  Java
关注(0)|答案(4)|浏览(124)

我试图测试我的一个名为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是final的,因此我不能模拟它。
我的期望是通过创建spy来模拟调用'deleteObject' ob Store的对象
下面是我的@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的对象,然后从Store的对象中创建一个间谍,并将其分配回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”,我得到了未完成的Mocking异常

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)


你知道我哪里搞错了吗?

hrysbysz

hrysbysz1#

我认为when中的第三个参数应该是一个mockito匹配器:

PowerMockito.doNothing().when(store, "deleteObject", eq(mockedObject));

字符串

x8diyxa7

x8diyxa72#

Mockito提供了一个方法doNothing()来模拟void方法的调用。对于上面的问题,void方法deleteFromRealm可以模拟为

Mockito.doNothing().when(mockedObject).deleteFromRealm();

字符串
因为deleteFromRealm是final,不能被嘲笑。模拟同类的deleteObject方法

ClassToBeTested testClass = new ClassToBeTested();
ClassToBeTested testClassSpy = PowerMockito.spy(testClass);
PowerMockito.doNothing().when(testClassSpy, "deleteObject", mockedRealm);

pepwfjgg

pepwfjgg3#

也许有点太晚了,但您的问题的可能解决方案可能看起来像这样:

PowerMockito.doNothing().when(store, "deleteObject", Matchers.any(RealmObject.class));

字符串
亲切的问候

txu3uszq

txu3uszq4#

您需要在test class的PrepareForTest注解中添加mock类,以便实际准备powermockito来模拟私有方法

public class A{

private void deleteObject(RealmObject object) {
        object.deleteFromRealm();
    }
}

个字符

相关问题