Take the 2-minute tour ×

I have an application that supports a few outbound web services. I am using HttpClient and I need to add a service time out because one of the external URLs that I am accessing takes a long time to respond (at times). I am trying to add a few junit test cases to make sure my timeout is working properly. I have something like this where the specified timeout value is little more than the configured socket timeout -

@Test (timeout=6000)
public void testTimeOut() {

I don't feel its a good one. Are there better tests I could use?

share|improve this question
Can you tell us what you don't like about the test? You're asking for improvement, but you don't say what the specific problem is. Also, if you're writing a test to make sure the Socket timeout is implemented correctly, that's kind of redundant, yes? You're not supposed to have to test other people's code (that's their responsibility) - you just need to test the code that you write. –  jefflunt Aug 30 '11 at 17:49
Whats wrong with this test? does it not work as you had expected? The way I see it is that your JUnit test should achieve the expected result, it does not have to be good or bad. –  Drupad Panchal Aug 30 '11 at 17:51
You are right. Perhaps that is a good enough test. I guess I wasn't sure if that was the only thing I could do to ensure that the timeout was working. The one thing I didn't add earlier was that my configured value is 5000 but to account for any other processing time, I set the timeout to 6000. Is that the right way to go about it? Thanks for the responses. –  spring5150 Aug 30 '11 at 18:23
add comment

1 Answer

If you are looking to write a true isolation unit test of your code then I agree that your test isn't appropriate. If you are looking to write more of an integration test then your approach is fine.

For my isolation junits I create them so that they only test the class under test. I use EasyMock to mock out all collaborating classes. So in this case I would create a mock for httpClient. I would set up a test where the mock throws the exception, and then assert that the class under test handles the exception as expected. The code snippets below may give you an idea of what I mean.

private DefaultHttpClient httpClient;
private ClassUnderTest classUnderTest;

public void setUpTest() {
     httpClient = createMock(DefaultHttpClient.class);

public void performARequestThatThrowsAnIOException() {


     try {
         fail("This test should throw an IOException.");
     } catch (IOException e){

The above assumes that the method in the class under test throws the exception. If the exception is caught then you would write the above differently.

share|improve this answer
add comment

Your Answer


