Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito Matchers: matching a Class type in parameter list

I am working with Java, Spring's RestTemplate, and Mockito, using Eclipse. I am trying to mock Spring's rest template, and the last parameter for the method I am mocking is a Class type. Below is the signature for the function:

public <T> ResponseEntity<T> exchange(URI url,
                                  HttpMethod method,
                                  HttpEntity<?> requestEntity,
                                  Class<T> responseType)
                       throws RestClientException

The initial attempt I made to mock this method is as follows:

//given restTemplate returns exception
when(restTemplate.exchange(isA(URI.class), eq(HttpMethod.POST), isA(HttpEntity.class), eq(Long.class))).thenThrow(new RestClientException(EXCEPTION_MESSAGE));

However, this line of code produces the following error from eclipse:

The method exchange(URI, HttpMethod, HttpEntity<?>, Class<T>) in the type RestTemplate is not applicable for the arguments (URI, HttpMethod, HttpEntity, Class<Long>)

Eclipse then suggests I cast the last parameter with a 'Class' cast, but does not seem to work if I cast it to a 'Class', or other type.

I've been looking online for help on this, but seem to stumped on the fact that the parameter requested is a class type.

The answers I've looked at so far have been mainly related to generic collections. Any help here would be greatly appreciated.

like image 597
piper1970 Avatar asked Sep 14 '15 21:09

piper1970


People also ask

How do you mock an object of a class?

We can use Mockito class mock() method to create a mock object of a given class or interface. This is the simplest way to mock an object. We are using JUnit 5 to write test cases in conjunction with Mockito to mock objects.

How do you use argument matchers in Mockito?

Mockito uses equal() as a legacy method for verification and matching of argument values. In some cases, we need more flexibility during the verification of argument values, so we should use argument matchers instead of equal() method. The ArgumentMatchers class is available in org. mockito package.

How do Mockito matchers work?

capture() it stores a matcher that saves its argument instead for later inspection. Matchers return dummy values such as zero, empty collections, or null . Mockito tries to return a safe, appropriate dummy value, like 0 for anyInt() or any(Integer. class) or an empty List<String> for anyListOf(String.

What is EQ Mockito?

Mockito Argument Matcher - eq() When we use argument matchers, then all the arguments should use matchers. If we want to use a specific value for an argument, then we can use eq() method. when(mockFoo. bool(eq("false"), anyInt(), any(Object. class))).


1 Answers

Figured out.

The method being called was a parameterized method, but could not infer the parameter type from the matcher argument (the last argument was of type Class).

Making the explicit call

when(restTemplate.exchange(isA(URI.class), eq(POST), isA(HttpEntity.class), eq(Long.class)))
        .thenThrow(new RestClientException(EXCEPTION_MESSAGE));

fixed my problem.

like image 193
piper1970 Avatar answered Sep 21 '22 13:09

piper1970