【问题标题】:Mocking Unmarshaller模拟解组器
【发布时间】:2016-08-02 17:57:24
【问题描述】:

我正在写一个JUnit 用于解组Object 的方法。它在解组对象时抛出IllegalArgumentException。我无法发布整个方法。

待测方法

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 为空,但我通过了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 ****" );
}

【问题讨论】:

  • 我还提到我正在传递一个参数,但它仍然给我同样的错误。
  • 如果我知道我通过了那我为什么要问这个问题。我知道 null 会抛出 NPE,但在这里我无法识别。如果您发现了问题,请告诉我。
  • 当它到达 unmarshall 方法时。 is 显示为空。
  • 是的,它包含 xml。
  • 你在哪里告诉代码使用你的模拟解组器?

标签: java junit jaxb mockito


【解决方案1】:

EISClientJAXBContextFactory.getUnmarshaller( GetPolicyResponse.class ) 是一个静态方法。您不能使用 Mockito 模拟静态方法。

【讨论】:

    猜你喜欢
    • 2011-05-24
    • 2011-11-12
    • 1970-01-01
    • 2017-01-28
    • 2023-04-03
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多