Unit testing JMockit-模拟CXF webservice端口对象
我试图对一个webservice包装类进行单元测试,该类试图隐藏所有webservice实现细节。它是从脚本平台调用的,因此所有接口都是简单的字符串或整数。该类提供了一个静态初始化方法,该方法接受主机名和端口号,并使用它创建一个私有的静态Apache CXF IRemote端口实例(由CXF wsdl2java生成)。对静态业务方法的后续调用将委托给端口实例 在对静态包装器类进行单元测试时,如何使用JMockit模拟CXF端口存根Unit testing JMockit-模拟CXF webservice端口对象,unit-testing,cxf,webservice-client,jmockit,Unit Testing,Cxf,Webservice Client,Jmockit,我试图对一个webservice包装类进行单元测试,该类试图隐藏所有webservice实现细节。它是从脚本平台调用的,因此所有接口都是简单的字符串或整数。该类提供了一个静态初始化方法,该方法接受主机名和端口号,并使用它创建一个私有的静态Apache CXF IRemote端口实例(由CXF wsdl2java生成)。对静态业务方法的后续调用将委托给端口实例 在对静态包装器类进行单元测试时,如何使用JMockit模拟CXF端口存根 public class WebServiceWrapper {
public class WebServiceWrapper {
private static final QName SERVICE_NAME = new QName("http://www.gwl.org/example/service",
"ExampleService");
private static IRemoteExample _port = null;
public static final String initialise(String url) {
if(_port != null) return STATUS_SUCCESS;
try {
URL wsdlURL = new URL(url);
ExampleService svc = new ExampleService(wsdlURL, SERVICE_NAME);
_port = svc.getIRemoteExamplePort();
BindingProvider bp = (BindingProvider)_port;
Map<String, Object> context = bp.getRequestContext();
context.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
return STATUS_SUCCESS;
}
catch(MalformedURLException ex) {
return STATUS_ERROR_PREFIX + ex.getMessage();
}
catch(WebServiceException ex) {
return STATUS_ERROR_PREFIX + ex.getMessage();
}
}
public static final String businessMethod(String arg) {
if(_port == null) {
return STATUS_ERROR_PREFIX + "Attempted to call businessMethod before connection is initialised. Pease call initialise first.";
}
try {
BusinessMethodRequest request = new BusinessMethodRequest ();
BusinessThing thing = new BusinessThing();
thing.setValue(arg);
request.setThing(thing);
BusinessMethodResponse response = _port.businessMethod(request);
String result = response.getResult();
if(result == null) {
return STATUS_ERROR_PREFIX + "Null returned!";
}
return STATUS_SUCCESS;
}
catch(MyBusinessException_Exception ex) {
return STATUS_ERROR_PREFIX + ex.getFaultInfo().getReason();
}
catch(WebServiceException ex) {
return STATUS_ERROR_PREFIX + ex.getMessage();
}
}
似乎工作如下 为我的BusinessMethodRequest添加了自定义匹配器类
class BusinessMethodRequestMatcher extends TypeSafeMatcher<BusinessMethodRequest> {
private final BusinessMethodRequestexpected;
public BusinessMethodRequestMatcher(BusinessMethodRequest expected) {
this.expected. = expected;
}
@Override
public boolean matchesSafely(BusinessMethodRequest actual) {
// could improve with null checks
return expected.getThing().getValue().equals(actual.getThing().getValue());
}
@Override
public void describeTo(Description description) {
description.appendText(expected == null ? null : expected.toString());
}
}
模拟对象现在使用正确的参数识别业务方法调用,并返回预期的响应对象。我想我已经做到了。我认为我的期望与传入的请求对象的特定实例不匹配。看起来我需要为我的BusinessMethodRequest对象提供一个自定义匹配器,它只关心请求。getThing().getValue()如果可以使用内置匹配器完成此操作,请呼喊。
class BusinessMethodRequestMatcher extends TypeSafeMatcher<BusinessMethodRequest> {
private final BusinessMethodRequestexpected;
public BusinessMethodRequestMatcher(BusinessMethodRequest expected) {
this.expected. = expected;
}
@Override
public boolean matchesSafely(BusinessMethodRequest actual) {
// could improve with null checks
return expected.getThing().getValue().equals(actual.getThing().getValue());
}
@Override
public void describeTo(Description description) {
description.appendText(expected == null ? null : expected.toString());
}
}
try {
port.createResource(with(req, new BusinessMethodRequestMatcher(req)));
returns(resp);
}