[英]Mocking Unmarshaller
I am writing a JUnit
for a method which unmarshalling an Object
. 我正在为解组Object
的方法编写JUnit
。 It is throwing IllegalArgumentException
while unmarshalling the Object.I cannot post the entire method. 它在解组对象时抛出IllegalArgumentException
。我无法发布整个方法。
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
在调试时,我发现它is
null,但我正在传递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 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 ****" );
}
EISClientJAXBContextFactory.getUnmarshaller( GetPolicyResponse.class )
is a static method. EISClientJAXBContextFactory.getUnmarshaller( GetPolicyResponse.class )
是一个静态方法。 You can't mock static methods using Mockito. 您不能使用Mockito模拟静态方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.