Search code examples
javajunitmockito

test void method using mockito and Junit with generic parameter


I am trying to test a void method using mockito and junit (the method contains a generic parameter). I would like to use the doNothing() for this method but after run the test will return exception for the method with generic parameter and for other with no generic param, it work;

And also the difference between the success and error is that : We define success method with final and not in the error method

The exception : Exception 1 :

You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains("foo"))

Also, this error might show up because you use argument matchers with methods that cannot be mocked. Following methods cannot be stubbed/verified: final/private/equals()/hashCode(). Mocking methods declared on non-public parent classes is not supported.

  Method to tests :
    
    public class GreenWork {
           @Autowired 
           Service service;
           public void getDataResponses() {
        
            ResponseEntity<DataDTO> responseEntity = restTemplate.exchange(
                            settings.getUrk(),
                            HttpMethod.GET,
                            null,
                            DataDTO.class);
            
            if (responseEntity.getStatusCode().is4xxClientError()) {
                ErrorData errorData = new ErrorData();
                errorData.setMessage("error loading");
                service.error("corId", errorData, "otherId");
            } else {
                service.success("corId", responseEntity.getBody(), "otherId");
            }
       }
    }
       
    **Service.java**
    public final void success(
            final String corId,
            final T t, // Generic class
            final String otherud)
            throws Exception {
        
        ......
        restTemplate.exchange(
            "url"),
            entity,
            Void.class);
    }
    
    
      public void error(
            final String corId,
            final ErrorData errorData,
            final String otherud)
            throws Exception {
        
        ......
        restTemplate.exchange(
            "url"),
            entity,
            Void.class);
    }
        
    My Junit tests :
        @InjectMocks
        private GreenWork greenWork;
        
    @Test
    public void shoulSuccessCallbak() {
        doNothing().when(service).success(anyString(), any(), anyString());  // Throw an exception Exception 1
        doNothing().when(service).success(anyString(), any(GrayDTO.class), anyString()); // With this I got also the same Exception
        doNothing().when(service).error("corId", any(ErrorData.class), "xId");  // No error no exception .. Work 
        greenWork.getDataResponses();
    }
    

Solution

  • Remove final modifier from your service

    public final void success()
    

    to

    public void success()