2016-08-02 12 views
0

Objectを非整列化する方法については、JUnitと書いています。 Objectをアンマーシャリングしている間にIllegalArgumentExceptionを投げています。メソッド全体をポストできません。デバッグ中、テスト中Mocking Unmarshaller

方法

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()); 

} 

私はisがnullであることがわかったが、私は

@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 ****"); 
} 
+0

私は引数を渡しているとも言いましたが、それでも私は同じエラーを与えています。 – Jaykumar

+0

私が渡していることを知っていたら、なぜこの質問をするのですか?私はnullがNPEを投げるのを知っていますが、ここで私は識別することができません。あなたが問題を特定したら、私に知らせてください。 – Jaykumar

+0

アンマーシャルメソッドに到達したとき。 isはnullを示します。 – Jaykumar

答えて

0

EISClientJAXBContextFactory.getUnmarshaller(GetPolicyResponse.class)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のは静的メソッドで渡しています。 Mockitoを使って静的メソッドをモックすることはできません。