Skip to content
Advertisement

How to create an argument captor for a Map object in mockito in java?

How to create an argument captor for Map<String, SomeCustomClass>?

I have the code that follows the following pattern:

JavaScript

And I want to test if the method doSomethingSubClass(mapSb) was called, whereby I need to be able to check with what argument it was called. For this purpose I have the following unit test:

JavaScript

The problem is that the above initialization of the argCaptor produces the following error message: “Cannot select from parametrized type”. Therefore, the question is how to declare an initialize in a correct way the argument captor for a map object like Map<String, SomeCustomeClass>? Thanks in advance!

Advertisement

Answer

You can do it either:

with @SuppressWarnings(“unchecked”)

JavaScript

or with junit5 and @Captor annotation:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement