JMockit - 模拟CXF webservice端口对象

时间:2014-05-07 05:17:42

标签: unit-testing cxf webservice-client jmockit

我试图对Web服务包装器类进行单元测试,该类尝试隐藏所有Web服务实现细节。它是从脚本平台调用的,因此所有接口都是简单的String或整数。该类提供静态初始化方法,该方法获取主机名和端口号,并使用它来创建私有静态Apache CXF IRemote端口实例(从CXF wsdl2java生成)。对静态业务方法的后续调用委托给端口实例。

在单元测试静态包装类时,如何使用JMockit模拟CXF端口存根?

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

如果我传递值&#34,那么web服务的示例行为就是&#34; OK&#34;然后它返回一条成功消息,但如果我调用的值为&#34; DUPLICATE&#34;然后webservice将抛出MyBusinessException_Exception。

我认为我已经设法模拟_port对象,但是业务调用总是返回一个null Response对象,所以我怀疑我的Expectations没有定义&#34; BusinessThing&#34;对象正确。我的测试方法到目前为止。

    @Test
public void testBusinessMethod(@Mocked final IRemoteExample port) {

    new NonStrictExpectations() {
        @Capturing IRemoteExample port2;
        {
            BusinessThing thing = new BusinessThing();
            thing.setValue("OK");
            BusinessMethodRequest req = new BusinessMeothdRequest();
            req.setThing(thing);

            BusinessMethodResponse resp = new BusinessMethodResponse ();
            resp.setResult("SUCCESS");

            try {
                port.businessMethod(req);
                returns(resp);
            }
            catch(MyBusinessException_Exception ex) {
                returns(null);
            }

            Deencapsulation.setField(WebServiceWrapper.class, "_port", port);
        }
    };

    String actual = WebServiceWrapper.businessMethod("OK");
    assertEquals(WebServiceWrapper.STATUS_SUCCESS, actual);
}

1 个答案:

答案 0 :(得分:2)

似乎工作如下。

为我的BusinessMethodRequest

添加了自定义Matcher类
    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());
    }
}

然后使用&#34;&#34;在我的期望中。

    try {
    port.createResource(with(req, new BusinessMethodRequestMatcher(req)));
    returns(resp);
}

模拟对象现在使用正确的参数识别业务方法调用,并返回预期的响应对象。