JUnit 4 – testing for exception, execution time and disabling a test

by Veera on July 4, 2009

in How To, Java

In my last post about Getting Started with JUnit 4, I talked about the basic concepts of JUnit testing framework and how to write a test case using JUnit 4. In this post, I’ll write about some tips and strategies for using JUnit effectively. We will start with JUnit annotation @Ignore.

Temporarily disabling a JUnit test:

In JUnit 4, it is possible to temperorily disable a test using the annotation @Ignore. This technique is very useful in scenarios where you are not done with that particular test case, but still want to run the other tests that are present in that test suite/class. The test methods which are annotated with both @Test and @Ignore will be ignored by the test runner. See the example below for a clear understanding.

@Test
public void testSomeMethod(){
        System.out.println("T'll be called");
}

@Ignore
@Test
public void testSomeOtherMethod(){
        System.out.println("T'll be ignored");
}

Testing for a specific Exception in JUnit testing method:

Suppose you want to check for a specific exception thrown by your testing method, you can use the expected option of @Test annotation. You can mention the Exception type that you expect the testing method to throw in the ‘expected’ option. If the method throws the exception then test is considered as passed, otherwise the test will be considered as failed.

@Test(expected=IllegalArgumentException.class)
public void testSomeMethod(){
   myObj.someMethod();
}

Testing for execution time of your method:

If you want to check if your testing method takes too long to execute, you can do that by mentioning your expected execution time using timeout option of @Test annotation. If your testing method takes longer than the time you mentioned, then the test will be considred as failed. The time is measured in milliseconds. Below is a code snippet for this.

@Test(timeout=2000)
public void testSomeMethod(){
   myObj.someMethod();
}

That’s for now. I hope that the above tips will help you t write effective JUnit tests. Feel free to share any other tips on JUnit. I’ll happy to hear from you.

Follow me on Twitter to get notified whenever I update this blog.

{ 4 comments }

abhay January 17, 2010 at 11:56 PM

if my function does not return any value… it just sets the value of some (local to function) variable ‘q’, and generates System.out.println statements at the end, how do i JUnit test this method … how do i Junit test print statements and some variable internal to function

Veera January 18, 2010 at 6:10 AM

I don’t think there is a easy way to do this. One solution I would suggest is:

1. Redirect your system.out.print messages to an external file, using System.setOut method.
2. From your JUnit read the output file and then parse to find the correct output pattern.

abhay January 18, 2010 at 7:25 AM

thanks for your reply, this makes sense for handling print statements, however the method to be tested also contains a local integer variable ‘q’ which is neither printed, nor returned nor its value saved in some variable that exists outside the function. any ideas on how i would access this variable that exists inside the function only. really appreciate the help

Veera January 18, 2010 at 9:45 PM

There are two ways to test a method which does not return anything.

1. As Junit FAQ says:

The method should have some side effect. i.e. it might alter some external files or data. In that scenario, it is better to test that external data to see the success of the method.

2. Use reflections to get the private variable, defined inside your method, in your JUnit test case and validate. Have a look at this article for more details: What’s the best way of unit testing private methods?

Comments on this entry are closed.

Previous post:

Next post: