1

I am writing a JUnit test case for a method in which I am getting a ClassCastException. I am getting this exception when the request object sets the entity. serviceRequest.setEntity((ResendDocumentsRequest)request);

Method under test

public Object invokeService(String key, Object request) {
   if( UiIntegrationKeyConstants.RESEND_DOCUMENTS.equals( key ) ) {
        ResendDocumentsServiceResponse serviceResponse = null;

        try {
            ESignatureClient client = (ESignatureClient) AppContext.getBean("eSignatureRestClient");
            ResendDocumentsServiceRequest serviceRequest = client.getResendDocumentsServiceRequest();
            serviceRequest.setEntity((ResendDocumentsRequest)request);

            serviceResponse = client.resendDocuments(serviceRequest);

        } catch (ESignatureClientException e) {
            log.error("Exception while retrieving documents Detail "+ e.getMessage());
        }
        return serviceResponse.getEntity();
    }
}

JUnit test case

private ResendDocumentsServiceRequest resendDocumentsServiceRequest;
private ResendDocumentsServiceResponse resendDocumentsServiceResponse;

@SuppressWarnings("unchecked")
@Before
public void setUP() {
    mockStatic( AppContext.class );
    esignatureClient = mock( ESignatureClient.class );
    resendDocumentsServiceRequest = mock( ResendDocumentsServiceRequest.class );
    resendDocumentsServiceResponse = mock( ResendDocumentsServiceResponse.class );
}

@Test
public void testESignatureClientException() {
    ESignatureServiceInvoker invoker = new ESignatureServiceInvoker();

    PowerMockito.when( AppContext.getBean( eq( "eSignatureRestClient" ) ) ).thenReturn( esignatureClient );
    when( esignatureClient.getResendDocumentsServiceRequest() ).thenReturn( resendDocumentsServiceRequest );
    try {
        doThrow( new ESignatureClientException() ).when( esignatureClient ).resendDocuments( resendDocumentsServiceRequest );
        resendDocumentsServiceResponse = ( ResendDocumentsServiceResponse ) invoker.invokeService( UiIntegrationKeyConstants.RESEND_DOCUMENTS , resendDocumentsServiceRequest );
    } catch ( ESignatureClientException ex ) {
        assertNotNull( ex );
    }
}

stack trace:

java.lang.ClassCastException: com.whatever.ResendDocumentsServiceRequest$$EnhancerByMockitoWithCGLIB$$15a76565 cannot be cast to com.whatever.ResendDocumentsRequest
Mike
  • 777
  • 3
  • 16
  • 41
  • You can tell mockito to mock additional interfaces, see https://stackoverflow.com/questions/1170708/is-it-possible-to-create-a-mock-object-that-implements-multiple-interfaces-with –  Sep 07 '17 at 17:00
  • I appreciate the quick comeback. Is there anything I could add to the answer to also make it upvote-worthy? – GhostCat Sep 08 '17 at 16:49

2 Answers2

1

You mocked your object to be a ResendDocumentsServiceRequest.

The production code expects an instance of ResendDocumentsRequest.

In other words: expected is A, provided is B. That would only work if B extends A, or B implements A. In that sense: this has nothing to do with mocking per se. But with the fact that you somehow provide an B object to your production code ... that should be of type A instead. When you do that at "real production runtime", too - you will run into the same error.

But we can't tell you A) why you did this nor B) what the correct solution is. As you stated that these are two disjunct classes - you have to figure if this fail is actually exposing a bug in your production code (by assuming that objects could be cast this way) - or in your test setup.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Short answer: The resendDocumentsServiceRequest object in your junit must extend or implement the ResendDocumentsRequest class or interface.

More Details:

  1. Create a static class (in the junit test class) that both extends the ResendDocumentsServiceRequest class and implements the ResendDocumentsRequest interface. I'll call this the TestKapow class.
  2. Change the type of the resendDocumentsServiceRequest object to TestKapow.
  3. Joy.
DwB
  • 37,124
  • 11
  • 56
  • 82
  • I am not really certain about your solution. ResendDocumentsRequest is not an interface and I can't extend two classes. – Mike Sep 07 '17 at 16:49
  • If that is the case, the the code (which is currently compiling) will not compile. Is ResendDocumentsServiceRequest an interface? If not, refer to the first sentence of this comment. – DwB Sep 07 '17 at 17:37