可以用Mockito来模拟org.jibx.runtime.BindingDirectory吗?

时间:2012-08-07 12:58:49

标签: java mockito

我正在为我的Java应用程序编写单元测试,我需要为可能抛出的潜在JiBX异常编写一个测试。我正在测试的方法调用来自另一个类的方法,其中可能会抛出JiBX异常。这是我正在测试的类(我们称之为A类):

@Inject
private CommonDAL commonDAL;

@Async
public Future<String> getTransactionalXXXAvailability(
        List<XXXAvailRequestEntry> requestEntries, TravelWindow travelWindow) {
    if (requestEntries.size() == 0)
        return null;
    XXXAvailRqAccessor requestAccessor = new XXXAvailRequestBuilder().buildRequest(requestEntries, travelWindow);
    logger.info(requestAccessor.marshalRequest());
    String responseAsXml = null;
    try {
        responseAsXml = getResponse(requestAccessor.getRequest());
    } catch (JiBXException e) {
        logger.error("Problem unmarshaling the XXX avail response: ", e);
    }

    logger.info(responseAsXml);
    return new AsyncResult<String>(responseAsXml);
}

private String getResponse(OTAXXXAvailRQ request) throws JiBXException {
    HbsiConnectionInfo connectionInfo = new HbsiConnectionInfo();
    connectionInfo.useConnectionInfoFromContext();

    HBSIXML4OTAWebserviceSoap hbsiSoap = getHbsiSoapService(connectionInfo);

    InterfacePayload header = new InterfacePayload();
    header.setChannelIdentifierId("XXXXXXXXX");
    header.setVersion("2005B");
    header.setInterface("HBSI XML 4 OTA");

    ComponentInfo componentInfo = new ComponentInfo();
    XXXAvailRqAccessor requestAccessor = new XXXAvailRqAccessor(request);
    componentInfo.setId(requestAccessor.getFirstXXXCode());
    componentInfo.setUser( connectionInfo.getUsername() );
    componentInfo.setPwd( connectionInfo.getPassword() );
    componentInfo.setComponentType(EComponentType.XXX);

    Login login = new Login();
    login.setComponentInfo(componentInfo);

    Message body = new Message();
    // todo: this needs to be unique for every request.
    // todo: hook up to logging
    body.setRequestId(UUID.randomUUID().toString());
    body.setTransaction(ETransaction.XXX_AVAIL_RQ);

    body.setXML(requestAccessor.marshalRequest());

    return hbsiSoap.getSoapRequest(header, body, login);
}

HBSIXML4OTAWebserviceSoap getHbsiSoapService(HbsiConnectionInfo connectionInfo) {
    HBSIXML4OTAWebservice ws = new HBSIXML4OTAWebservice( connectionInfo.getWsdlLocation() );

    HBSIXML4OTAWebserviceSoap hbsiSoap = ws.getHBSIXML4OTAWebserviceSoap();
    Map<String, Object> requestContext = ((BindingProvider)hbsiSoap).getRequestContext();
    String readTimeout = commonDAL.getPropertyValue(new PropertyKey(Section.HBSI,
            Property.HBSI_WS_READ_TIMEOUT));
    requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, Integer.parseInt(readTimeout));
    String connectionTimeout = commonDAL.getPropertyValue(new PropertyKey(Section.HBSI,
            Property.HBSI_WS_CONNECTION_TIMEOUT));
    requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, Integer.parseInt(connectionTimeout));
    return hbsiSoap;
}

抛出错误的方法如下(从另一个类开始,我们称之为B类):

public String marshalRequest() {
    StringWriter requestAsXml = new StringWriter();

    try {
        IBindingFactory bindingFactory = BindingDirectory.getFactory(PROTECTEDCLASSNAME.class);
        IMarshallingContext marshalingContext = bindingFactory.createMarshallingContext();
        marshalingContext.setIndent(2);
        marshalingContext.setOutput(requestAsXml);
        marshalingContext.marshalDocument(request);

    } catch (JiBXException e) {
        logger.error("Problem marshaling PROTECTEDCLASSNAME.", e);
    }

    return requestAsXml.toString();
}

当“body.setXML(requestAccessor.marshalRequest());”如果调用了另一个类(requestAccessor),它的方法.marshalRequest是应该抛出JiBX异常的地方。我正在编写的测试的目的是让这个A类的单元测试覆盖率达到100&amp;,但是被测系统至少由两个类组成,因为我无法模拟名为requestAccessor的XXXAvailRqAccessor对象。由于以下原因,我无法进行任何测试来产生此错误。

  • 名为requestAccessor的XXXAvailRqAccessor对象在我正在测试的方法中实例化,所以我不能使用mock来抛出异常。

  • 传递给.getResponse()的OTAXXXAvailRQ参数不能被模拟,因为它是由构建器为XXXAvailRqAccessor创建的。

  • 我试过间谍IBindingFactory,但它没有用。我在B类中创建了一个实例化IBindingFactory的方法,所以我可以监视它,但这不起作用。

  • 我还尝试使用PowerMock在实例化时返回模拟XXXAvailRqAccessor,但是当我尝试模拟.getRequest的JiBXExceptioin时,Mockito说“Checked exception对此方法无效”。如果我不能让Mockito抛出这个错误,我不知道是否可以操纵相关的对象来抛出它。

2 个答案:

答案 0 :(得分:2)

嗯,不是真的,或者我至少不知道这样的方式。你可以,如果你真的想要这样做(我反对它)在这个类中创建一个这样的方法:

IBindingFactory getBindingFactory() {
    return BindingDirectory.getFactory(PROTECTEDCLASSNAME.class);
}

并替换此行:

IBindingFactory bindingFactory = BindingDirectory.getFactory(PROTECTEDCLASSNAME.class);

使用:

IBindingFactory bindingFactory = getBindingFactory();

然后你可以窥探()(你可以在文档中阅读Mockito.spy(),如果你不熟悉它)这个对象并使这个方法返回一个模拟。从那时起,它一帆风顺。

不建议使用这种方法,因为:

  1. 您正在创建一个仅用于测试的新方法(无用的方法)
  2. 所述方法必须可见才能进行测试,因此您无法将其标记为私有...
  3. 我不是一般的间谍粉丝
  4. 问题仍然存在:如何正确测试此类案例。在大多数情况下,我尽可能地尝试重构,有时它会有所帮助。而在其他情况下......我仍然没有找到更好的解决方案。

答案 1 :(得分:1)

正如我在评论中所说,我完全提倡Mateusz的解决方案。但还有另一种选择。在具有marshalRequest方法的类中,拥有类型为IBindingFactory的私有final字段。同样在这个类中,有一个带有一个额外参数的package-private构造函数,即要设置的IBindingFactory。普通构造函数将调用BindingDirectory.getFactory( ... ),然后调用新的构造函数。因此,如果标准构造函数具有单个String参数,则该类可能如下所示。

public class MyClass{
    private String name;
    private IBindingFactory bindingFactory;

    public MyClass(String name){
        this(name, BindingDirectory.getFactory(PROTECTEDCLASSNAME.class));
    }

    MyClass(String name, IBindingFactory bindingFactory){
        this.name = name;
        this.bindingFactory = bindingFactory;
    }

    public String marshalRequest() {
        StringWriter requestAsXml = new StringWriter();

        try {
            IMarshallingContext marshalingContext = bindingFactory.createMarshallingContext();
            marshalingContext.setIndent(2);
            marshalingContext.setOutput(requestAsXml);
            marshalingContext.marshalDocument(request);

        } catch (JiBXException e) {
            logger.error("Problem marshaling PROTECTEDCLASSNAME.", e);
        }

        return requestAsXml.toString();
    }
}

这样做的原因是,在您的类的测试中,您可以创建模拟IBindingFactory,并将其传递给package-private构造函数。