简体   繁体   English

使用Mockito验证是否使用包含子字符串的参数调用了方法

[英]Using Mockito verify a method was called with an argument containing a substring

When logger.error is called, there are many different things that can be passed as the 3rd argument. 调用logger.error ,有许多不同的东西可以作为第三个参数传递。

How can I verify that e contains a specific substring? 如何验证e包含特定的子字符串?

This is my production code .. 这是我的生产代码..

public class MyClass {

    private ILogger logger = Slf4jLogbackLogger
            .generateLogger(MyClass.class.getClassLoader().getResource("log_messages.properties"));

    public void doSomething() {
        logger.info(Optional.empty(), "MyClass.doSomething");

        try {
            .. do things // throw new RuntimeException("something");

        } catch (Exception e) {
            logger.error(Optional.empty(), "HANDLE_EXCEPTION", e);
        }
    }
}

My test code .. 我的测试代码..

@RunWith(MockitoJUnitRunner.class)
public class TestMyClass {

    @Mock
    private ILogger logger;

    @InjectMocks
    @Spy
    private MyClass myClass = new MyClass();

    @Before
    public void init() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testMyClass() throws Exception {

        try {
            myClass.doSomething();
        } catch (Exception e) {
            fail("Should not have thrown any exceptions");
        }
        Mockito.verify(logger, Mockito.times(1)).error(Optional.empty(), "HANDLE_EXCEPTION", "Contains something specific");
    }
}

This is the error I'm recieving .. 这是我收到的错误。

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
3 matchers expected, 1 recorded:

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

Have tried things like below, but same error: 尝试过类似以下的操作,但存在相同的错误:

ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
Mockito.verify(logger, Mockito.times(1)).error(Optional.empty(), "HANDLE_EXCEPTION", argument.capture());

All arguments during veryfying that error method was called on logger need to be matchers in your case. 在这种情况下,在logger调用该error方法的过程中,所有参数都必须matchers Here is an example of using matchers and mocking some Service class : 这是一个使用matchers并模拟一些Service类的示例:

@ExtendWith(MockitoExtension.class)
public class SomeTestClass {

    private static class MyService {
        public void error(Optional<String> op, String msg1, String msg2) {

        }
    }

    @Test
    void testMethod() {
        MyService myService = Mockito.mock(MyService.class);
        myService.error(Optional.empty(), "This is error", "Some error appeared");
        Mockito.verify(myService, Mockito.times(1)).error(Mockito.any(), Mockito.eq("This is error"), Mockito.contains("error"));
    }
}

Here Mockito.any() , Mockito.eq() , Mockito.contains() are methods that return matchers. 这里Mockito.any()Mockito.eq()Mockito.contains()是返回匹配器的方法。

To assert that method was invoked with String containing given substring use Mockito.contains() matcher. 要断言该方法是使用包含给定子字符串的String调用的,请使用Mockito.contains()匹配器。 Here you can find more information about using Matchers . 在这里,您可以找到有关使用Matchers的更多信息。

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

相关问题 使用 Object 参数验证方法未使用 Mockito 调用 - Verify method with Object argument not called using Mockito 使用Mockito,如何验证方法是否具有某个参数? - Using Mockito, how do I verify a method was a called with a certain argument? Mockito验证使用正则表达式使用正确的参数调用方法 - Mockito verify that method is called with correct argument using regex 如何验证未使用 Mockito 调用特定方法? - How to verify that a specific method was not called using Mockito? Mockito验证方法一次调用 - Mockito verify method called once 有没有一种方法可以验证使用Mockito调用了Spring Controller上的方法 - Is there a way to verify that a method on Spring Controller was called using Mockito 如何使用Mockito验证未使用任何参数组合调用的模拟方法 - How to verify mocked method not called with any combination of parameters using Mockito 如何使用 JUnit Mockito 验证未调用方法 - How to check that a method is not being called using JUnit Mockito Verify 我们可以使用 Mockito 验证在几个对象之一上调用了一个方法吗 - Can we verify a method is called on one of the several objects using Mockito 如何验证Mockito是否使用期望的参数(以java.util.List作为参数)调用了方法 - How to verify if method was called with expected argument (java.util.List as an argument) by Mockito
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM