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

  • Home
  • SEARCH
  • 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 6909477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:41:40+00:00 2026-05-27T08:41:40+00:00

I have some unit tests which exercise code which makes calls out to a

  • 0

I have some unit tests which exercise code which makes calls out to a test server, in order to make sure that the requests are well-formed (i.e. we get valid data back in response). However, this means that the unit tests, and hence the build, can get blocked if this test server is down. This does not conform to good unit test practices, but as a thought experiment let’s say I’m not allowed to delete these tests or change them so they don’t actually call out to the server. I want to change them so that they will still pass if the server is down (i.e. trying to connect results in ConnectException), but fail if any other exception occurs. Making it more difficult, the code under test doesn’t throw the ConnecException directly, but throws a wrapper exception that contains it. So initially, that means each test will go from looking like this:

@Test
public void testNumberOne() {
    // body of test...
}

To this:

@Test
public void testNumberOne() {
    try {
        // body of test...
    } catch (ThirdPartyWrapperException e) {
        if (!(e.getRootCause() instanceof ConnectException) {
            throw e;
        }
    }
}

Is there any way I can avoid having to paste that try/catch into each unit test?

I know I can refactor out at least some of it, ala:

@Test
public void testNumberOne() {
    try {
        // body of test...
    } catch (ThirdPartyWrapperException e) {
        handleException(e);
    }
}

private void handleException(ThirdPartyWrapperException e) 
             throws ThirdPartyWrapperException {
    if (!(e.getRootCause() instanceof ConnectException) {
        throw e;
    }
}

But is there anything further I can do?

  • 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-05-27T08:41:40+00:00Added an answer on May 27, 2026 at 8:41 am

    I would add a line to the start to determine if the required resources are available

    @Test
    public void testNumberOne() {
        if (!requiredServerAvailable()) return;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some unit tests running against a SQL server 2000 database using the
I have some written a number of unit tests that test a wrapper around
I'm using Google Test Framework to set some unit tests. I have got three
We have some unit tests which work with performance counters (specifically creating new categories)
We have some unit tests which compile and run fine in Eclipse 3.4, but
I have a class which has some unit tests, but when I am running
I am writing some unit tests which I have simplified to the following: class
I used to have a standalone script with some unit tests to test data
We've all been there. You have written some code and unit tests, the tests
I have a ListTester.java file in which I've created with some unit tests in

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.