简体   繁体   English

无法在Eclipse中使用Mockito验证方法调用

[英]Cannot verify method calls with mockito in eclipse

I am trying to use Mockito to do some verifications about the number of times a method was called, but the syntax that I'm seeing in all the tutorials I can find is causing an "Unresolved compilation problem" error in eclipse. 我正在尝试使用Mockito对方法被调用的次数进行一些验证,但是我在所有可以找到的教程中看到的语法都导致Eclipse中出现“未解决的编译问题”错误。 The class I'm mocking is: 我嘲笑的类是:

public class ClassToBeMocked {
    public void methodToVerify(String input) {

    }
}

The test is: 测试是:

@RunWith(MockitoJUnitRunner.class)
public class MockitoTest {
    @Mock ClassToBeMocked mockedClass;

    @Before
    public void setUp() throws Exception {
        this.mockedClass = Mockito.mock(ClassToBeMocked.class);
    }

    @Test
    public void test() {
        Mockito.verify(mockedClass, Mockito.never()).methodToVerify();
    }
}

But when I try to run the test in eclipse, I get this error: 但是当我尝试在Eclipse中运行测试时,出现此错误:

java.lang.Error: Unresolved compilation problem: 
The method methodToVerify(String) in the type ClassToBeMocked is not applicable for the arguments ()

at MockitoTest.test(MockitoTest.java:28)
at . . . 

At first blush, the error seems logical: after all, it appears as though I am calling a method without passing in the required parameters. 乍一看,该错误似乎是合乎逻辑的:毕竟,似乎我在调用方法而不传递必需的参数。 But I thought Mockito would do some under-the-hood work to make it work. 但是我认为Mockito会做一些幕后工作来使其工作。

If my use of the framework is wrong, then how should I be verifying this method? 如果我对框架的使用不正确,那么应该如何验证此方法?

I'm using Mockito version 2.7.6. 我正在使用Mockito版本2.7.6。 I have not tried running the test outside of eclipse because I'm new to java/junit/mockito/maven, and I haven't yet figured out how to run the tests from the terminal. 我没有尝试在eclipse之外运行测试,因为我是java / junit / mockito / maven的新手,并且我还没有弄清楚如何从终端运行测试。

您需要包括一个参数匹配器,例如anyString()

Mockito.verify(mockedClass, Mockito.never()).methodToVerify(Mockito.anyString());
 Mockito.verify(mockedClass, Mockito.never()).methodToVerify(anyString());

使用以上句子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM