Alex Staveley is a software professional passionate about software engineering and technical architecture. He blogs about architectural approaches, Java topics, web solutions and various technical bits and pieces. Alex is a DZone MVB and is not an employee of DZone and has posted 48 posts at DZone. You can read more from them at their website. View Full User Profile

Doing more with less in JUnit 4

01.15.2012
| 4772 views |
  • submit to reddit

JUnit is a popular testing framework for writing unit tests in Java projects. Every software engineer should always try to consider using less code to achieve objectives and the same is true for test code. This post shows one way to write less test code by increasing code re-use using JUnit 4 annotations.
Ok, so let's set the scene. You have a bunch of tests that are very similar but vary only slightly in context. For example, let's say you have three tests which do pretty much the same thing. They all read in data from a file, and count the number of lines in the file. Essentially you want the one test to run three times, but read
a different file and test for a specific amount of lines each time.

This can be achieved in an elegant way in JUnit 4 using the @RunWith and @Parameters annotations. An example is shown below.

@RunWith(Parameterized.class)
public class FileReadTest {
   @Parameters
   public static Collection<Object[]> data() {
            return Arrays.asList(
              new Object[][]{{"players10.txt", 10}, {"players100.txt", 100},
   {"players300.txt", 300}});
   }
 
   private String fileName;
   private int expectedNumberOfRows;
 
   public FileReadTest(String fileName, int expectedNumberOfRows) {
       this.fileName = fileName;
       this.expectedNumberOfRows = expectedNumberOfRows;
   }
 
   @Test
   public testFile() {
       LineNumberReader  lnr = new LineNumberReader(new FileReader(fileName)));
       lnr.skip(Long.MAX_VALUE);
       assertEquals("line number not correct", expectedNumberOfRows,                    lnr.getLineNumber());
   }
}

W.R.T. to code above:

  1. The RunWith annotation tells JUnit to run with Parameterized runner instead of the default runner.
  2. The @Parameters annotation is defined by the JUnit class org.junit.runners.Parameterized.
    It is used to define the parameters that will be injected into the test.
  3. The data() method is annotated with the parameters annotation. It defines a two dimensional array. The first array is: "players10.txt", 10. These parameters will be used the first time the test is run. "players10.txt" is the file name. 10 is the expected number of rows.
So hopefully this makes sense. Not only is this a way to cut down on test code, it is also a way to provide extra testing of your code. Every time you write a test all you have to do is ask yourself if it makes sense to parameterize your test.
Enjoy...

 

From http://dublintech.blogspot.com/2012/01/doing-more-with-less-in-junit-4.html

Published at DZone with permission of Alex Staveley, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Tim Bb replied on Sun, 2012/01/15 - 6:59pm

So when would you use junit Theories instead of using a parametrized test?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.