Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:38:11+00:00 2026-06-09T05:38:11+00:00

I’m writing unit tests for my Java app and I need to write a

  • 0

I’m writing unit tests for my Java app and I need to write a test for a potential JiBX exception that can be thrown. The method I’m testing calls on a method from another class, where the JiBX exception can be potentially thrown. This is the class I’m testing (let’s call it Class 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;
}

The method that throws the error is as follows (and from another class, let’s call it Class 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();
}

When “body.setXML(requestAccessor.marshalRequest());” is called, another class (requestAccessor) is visited by the test, and it’s method .marshalRequest is where the JiBX exception is supposed to be thrown. The purpose of the tests I’m writing is to get this Class A’s unit test coverage to 100&, but the system under test is composed of at least two classes as I can’t mock XXXAvailRqAccessor object called requestAccessor. I can’t get any tests to produce this error, for the following reasons.

  • The XXXAvailRqAccessor object called requestAccessor is instantiated inside the methods I’m testing so I can’t use a mock to throw an exception.

  • the OTAXXXAvailRQ argument passed to .getResponse() cannot be mocked because it’s created by the builder for XXXAvailRqAccessor.

  • I tried spying on IBindingFactory, but it didn’t work. I created a method in Class B that would instantiate an IBindingFactory so I could spy on it, but that didn’t work.

  • I also tried using PowerMock to return a mock XXXAvailRqAccessor when it is instantiated, however when I attempted to mock a JiBXExceptioin for .getRequest, Mockito said “Checked exception is invalid for this method”. If I can’t get Mockito to throw this error, I don’t know if it’s possible to manipulate the associated objects to throw it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:38:13+00:00Added an answer on June 9, 2026 at 5:38 am

    Well not really, or I don’t know of such way at least. You could, if you REALLY want to do it (I’m against it) create a method like this in that class:

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

    And replace this line:

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

    With:

    IBindingFactory bindingFactory = getBindingFactory();
    

    Then you can spy() (you can read on Mockito.spy() in the documentation if you’re not familiar with it) this object and make this method return a mock. From that point it’s smooth sailing.

    This approach is not adviced though because:

    1. you are creating a new method (a useless one) just for testing
    2. said method has to be visible for testing so you cannot mark it as private…
    3. I’m not a huge fan of spies in general

    The question remains: how to properly test such cases. Well in most situations I try to refactor as much as possible and sometimes it helps. And in other cases… Well I still did not come up with a better solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.