0

I am writing a JUnit for a method which unmarshalling an Object. It is throwing IllegalArgumentException while unmarshalling the Object.I cannot post the entire method.

Method under test

public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
    MultivaluedMap<String, String> headers = responseContext.getHeaders();
    String contentType = headers.getFirst( CONTENT_TYPE_HEADER );

    if( contentType != null && contentType.startsWith( TEXT_XML ) ) {
        if( 200 == responseContext.getStatus() ) {
            if( requestContext.getUri().toString().contains( "Policy/Retrieve" ) ) {
                try {
                    GetPolicyResponse p = (GetPolicyResponse)EISClientJAXBContextFactory.getUnmarshaller( GetPolicyResponse.class ).unmarshal( responseContext.getEntityStream() );

}

While debugging I found that is is null but I am passing inputStream

public final Object unmarshal( java.io.InputStream is )
    throws JAXBException {

    if( is == null ) {
        throw new IllegalArgumentException(
            Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) );
    }

    InputSource isrc = new InputSource( is );
    return unmarshal( isrc );
}

JUnit

@Test
public void testPersonalAutoDriver() throws Exception {
 ClientRequestContext requestContext = mock(ClientRequestContext.class);
    ClientResponseContext responseContext = mock(ClientResponseContext.class);
    MultivaluedMap<String, String> headers = mock(MultivaluedMap.class);
    URI uri = new URI("Policy/Retrieve");
    Unmarshaller unmarshaller = mock(Unmarshaller.class);

    when( responseContext.getHeaders() ).thenReturn( headers );
    when( headers.getFirst(Mockito.eq( "Content-Type" )) ).thenReturn( "text/xml" );
    when( responseContext.getStatus() ).thenReturn( 200 );
    when( requestContext.getUri()).thenReturn(uri);

    JAXBContext context = JAXBContext.newInstance(GetPolicyResponse.class);
    Marshaller marshaller = context.createMarshaller();

    Policy policy = new Policy();
    policy.setPolicyType( PolicyTypeEnum.PERSONAL_AUTO );

    GetPolicyResponse policyResponse = new GetPolicyResponse();
    policyResponse.setPolicy(policy);

    StringWriter writer = new StringWriter();
    marshaller.marshal( policyResponse, writer );
    System.out.println( "**** Object Marshalled Successfully ****" );

    InputStream inputStream = new ByteArrayInputStream(writer.toString().getBytes());
    when(unmarshaller.unmarshal(inputStream)).thenReturn(policyResponse);

//  EISClientJAXBContextFactory.getUnmarshaller( GetPolicyResponse.class );
//  Unmarshaller unmarshaller = context.createUnmarshaller();
//  GetPolicyResponse policyResponse2 = ( GetPolicyResponse ) unmarshaller.unmarshal( inputStream );

//  assertNotNull(policyResponse2);

    filter.filter( requestContext, responseContext );
    System.out.println( "**** Object Unmarshalled Successfully ****" );
}
Jaykumar
  • 165
  • 1
  • 8
  • 23
  • I also mentioned that I am passing an argument but still it is giving me the same error. – Jaykumar Aug 02 '16 at 18:08
  • If I knew that I am passing then why would I ask this question. I know null throws NPE but here I am unable to identify. If you have identified the issue let me know. – Jaykumar Aug 02 '16 at 18:14
  • When it reaches to unmarshall method. is shows null. – Jaykumar Aug 02 '16 at 18:16
  • Yes it contains the xml. – Jaykumar Aug 02 '16 at 18:37
  • writer = PersonalAuto – Jaykumar Aug 02 '16 at 18:38
  • No, I've commented out those lines because I tried another way but it didn't work that way either. – Jaykumar Aug 02 '16 at 18:44
  • 1
    Where do you tell the code to use your mocked unmarshaller? – DaveH Aug 02 '16 at 18:47
  • I am not sure. I am assuming when I invoke the unmarshal method using mocked unmarshaller object. – Jaykumar Aug 02 '16 at 18:53
  • At the moment you have created an unmarshaller object but it will not be used. You need to get `(GetPolicyResponse)EISClientJAXBContextFactory.getUnmarshaller` to return your unmarshaller object - which is not trivial ( if you are using Mockito - as it appears to me you are). Mockito can't mock static methods, but a mixture of PowerMock and mockito can. Google "mockito mock static methods" - it's fairly complex though – DaveH Aug 02 '16 at 19:00
  • I know PowerMock would make it even harder therefore I was trying to keep it simple. – Jaykumar Aug 02 '16 at 19:03
  • Well I think you've had it .. if you want to execute the line `GetPolicyResponse p = (GetPolicyResponse)EISClientJAXBContextFactory.getUnmarshaller( GetPolicyResponse.class ).unmarshal( responseContext.getEntityStream() );`, then you're going to need to use a mocking framework that will mock static methods – DaveH Aug 02 '16 at 19:10
  • This may help a bit ... http://stackoverflow.com/questions/17083432/mock-objects-calling-final-classes-static-methods-with-mockito/17084152#17084152 – DaveH Aug 02 '16 at 19:12
  • Thanks Dave, Ii think this is the only route to go with. – Jaykumar Aug 02 '16 at 19:14
  • It worked. PowerMock with Mockito took care of it. Thanks all. – Jaykumar Aug 02 '16 at 21:36

1 Answers1

0

EISClientJAXBContextFactory.getUnmarshaller( GetPolicyResponse.class ) is a static method. You can't mock static methods using Mockito.