Junit 使用Mockito验证来自另一个类的方法被调用

Junit 使用Mockito验证来自另一个类的方法被调用,junit,mockito,Junit,Mockito,我遇到了一个PIT突变问题,如果它删除class.callVoidMethod(),它就会存活下来。我假设我只需要验证调用是否已发出,但我很难让测试用例变为绿色。我曾尝试过间谍和嘲弄,但我发现与这个嘲弄没有任何互动。当调试时,我看到它仍在调用真正的方法。知道如何让它使用模拟软件吗 测试方法: public void someMethod(String word) { word = "class" SomeClass class = new SomeClass();

我遇到了一个PIT突变问题,如果它删除class.callVoidMethod(),它就会存活下来。我假设我只需要验证调用是否已发出,但我很难让测试用例变为绿色。我曾尝试过间谍和嘲弄,但我发现与这个嘲弄没有任何互动。当调试时,我看到它仍在调用真正的方法。知道如何让它使用模拟软件吗

测试方法:

public void someMethod(String word)  
{
    word = "class"
    SomeClass class = new SomeClass();  
    class.callVoidMethod(word, "char");  
}
测试用例:

@InjectMocks
ClassUnderTest underTest;   
@Mock
SomeClass class;

@Test
public void testSomeMethod()
{
    underTest = new ClassUnderTest();
    //Not Sure if I need this
    Mockito.doCallRealMethod()
        .when(class).callSomeVoidMethod(anyString());
    underTest.someMethod("test");
    Mockito.verify(class).callSomeVoidMethod(anyString());
}

下面是我在本地运行的一个示例。除了一些命名问题外,主要的问题是您实际上没有模仿
SomeClass
。您的测试方法是创建并调用一个真实实例。此外,在使用runner时,不需要创建测试主题的实例

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class VerifyTest {

    @InjectMocks
    private ClassUnderTest underTest;

    @Mock
    private SomeClass clazz;

    @Test
    public void testSomeMethod() {
        underTest.someMethod("test");
        Mockito.verify(clazz).callVoidMethod("test", "char");
    }

    private static class ClassUnderTest {

        private SomeClass clazz;

        public void someMethod(String word) {
            clazz.callVoidMethod(word, "char");
        }
    }

    private static class SomeClass {
        public void callVoidMethod(String word, String otherWord) {
            // do nothing
        }
    }
}

class
是保留关键字。那是打字错误吗?