最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to use anyString() inside a Map for Mockito verification? - Stack Overflow

programmeradmin3浏览0评论

I'm writing a unit test in a Spring Boot application using Mockito and JUnit 5, and I'm trying to verify a method call where one of the parameters is a Map<String, String>.

Here's the method signature:

public void someMethod(SomeClass obj, String parameter1, Map<String, String> properties) { ... }

I need to verify that this method was called with a Map containing certain keys (property2 and property3) but with any string value.

Here's my test code:

@Test
void testMethodVerification() {
    Map<String, String> properties = Map.of(
        "property1", "TEST_VALUE",
        "property2", anyString(), // This causes an error
        "property3", anyString() // This causes an error
    );

    verify(mockedService, times(1)).someMethod(
        any(SomeClass.class),
        eq("some_attribute"),
        eq(properties) // Issue occurs here
    );
}

However, I'm getting this error:

.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:
-> at com.mycompany.myproject.MyTestClass.testMethodVerification(MyTestClass.java:XX)

Question: How can I verify that someMethod was called with a Map where "property2" and "property3" can be any string?

Would appreciate any guidance!

I tried using anyString() inside Map.of() to allow flexibility in verifying property2 and property3. However, I encountered an UnfinishedVerificationException because anyString() is an argument matcher and cannot be used directly inside Map.of().

I expected Mockito to verify that the method was called with a Map containing "property1" as a fixed value and "property2" and "property3" as any string values. However, the test failed due to incorrect matcher usage.

Now, I’m looking for the correct way to verify a method call where certain Map values can be any string, while other keys must match specific values.

I'm writing a unit test in a Spring Boot application using Mockito and JUnit 5, and I'm trying to verify a method call where one of the parameters is a Map<String, String>.

Here's the method signature:

public void someMethod(SomeClass obj, String parameter1, Map<String, String> properties) { ... }

I need to verify that this method was called with a Map containing certain keys (property2 and property3) but with any string value.

Here's my test code:

@Test
void testMethodVerification() {
    Map<String, String> properties = Map.of(
        "property1", "TEST_VALUE",
        "property2", anyString(), // This causes an error
        "property3", anyString() // This causes an error
    );

    verify(mockedService, times(1)).someMethod(
        any(SomeClass.class),
        eq("some_attribute"),
        eq(properties) // Issue occurs here
    );
}

However, I'm getting this error:

.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:
-> at com.mycompany.myproject.MyTestClass.testMethodVerification(MyTestClass.java:XX)

Question: How can I verify that someMethod was called with a Map where "property2" and "property3" can be any string?

Would appreciate any guidance!

I tried using anyString() inside Map.of() to allow flexibility in verifying property2 and property3. However, I encountered an UnfinishedVerificationException because anyString() is an argument matcher and cannot be used directly inside Map.of().

I expected Mockito to verify that the method was called with a Map containing "property1" as a fixed value and "property2" and "property3" as any string values. However, the test failed due to incorrect matcher usage.

Now, I’m looking for the correct way to verify a method call where certain Map values can be any string, while other keys must match specific values.

Share Improve this question asked Mar 14 at 22:07 samsam 11 silver badge 1
  • 1 you can't, anyString is used in mocks, not concrete classes like a HashMap, you will have to mock the entire hashmap. Cant give any more information since you have provided too little information. – Toerktumlare Commented Mar 14 at 22:57
Add a comment  | 

1 Answer 1

Reset to default 4

anyString() only works if you use it inside a when or verify call. If you use it outside, it will simply return the empty string "" (and corrupt Mockito's internal matcher stack).

If you need to perform custom matching, argThat or a custom ArgumentMatcher implementation work:

verify(mockedService).someMethod(
    any(SomeClass.class),
    eq("some_attribute"),
    argThat(arg -> Objects.equals(arg.get("property1"), "TEST_VALUE")
        && arg.containsKey("property2")
        && arg.containsKey("property3")));

A custom implementation allows you to override its toString method, which is very useful if your verification failed and you want to get readable failure messages.

class MapMatcher implements ArgumentMatcher<Map<String, String>> {
  @Override
  public boolean matches(final Map<String, String> map) {
    return Objects.equals(map.get("property1"), "TEST_VALUE")
        && map.containsKey("property2")
        && map.containsKey("property3")
  }

  @Override
  public String toString() {
    return "{property1=>TEST_VALUE,property2=>*,property3=>*}";
  }
}

Usage of the custom matcher:

verify(mockedService).someMethod(
    any(SomeClass.class),
    eq("some_attribute"),
    argThat(new MapMatcher()));
发布评论

评论列表(0)

  1. 暂无评论