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
2
  • You can tell mockito to mock additional interfaces, see stackoverflow.com/questions/1170708/… Commented Sep 7, 2017 at 17:00
  • I appreciate the quick comeback. Is there anything I could add to the answer to also make it upvote-worthy? Commented Sep 8, 2017 at 16:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

I am not really certain about your solution. ResendDocumentsRequest is not an interface and I can't extend two classes.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.