About Anuj Kumar

Anuj is working as a Senior S/W Engineer in Netherlands. He is a Sun Certified Java Professional with experience in design and development of mid/large scale Java applications. He is the creator of EasyTest Framework(https://github.com/EaseTech) which is a Data Driven Testing Framework for Java.

Run your Unit Tests in Parallel

It was about time when the developer of Unit Tests had the ability to run the tests in Parallel using annotations. In today’s blog post, we will look at how you can make your traditional Junit Tests to run in parallel using annotations provided by Easytest. EasyTest  is a Testing Framework build on top of JUnit to provide you ease of writing and maintaining your tests. It is focused on writing Data Driven Tests for your application.

Lets start by assuming a Class ItemService  that has a method 2 methods:
 
 
 

  1. getItems that takes 2 arguments Double itemId and String itemType. The API class is described below.
  2. updateItem that takes an Item instance and updates it in the Database.
public class ItemService {

   public List getItems (Double itemId , String itemType);

   public void updateItem (Item item);

}

We will leave out the implementation for the sake of keeping the example simple.

Lets see the step by step approach of writing Parallel Unit Tests.

STEP 1:

Download the latest version(or version 1.2 and above) of EasyTest from Maven Central Repository.

STEP 2:

Next, lets write a simple Unit Test for the above business methods using EasyTest annotations and data driven testing approach.

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths="getItemData.csv")
@TestConfigProvider(TestConfigProvider.class)
public class ItemServiceTest {

     @Inject
     private ItemService itemService;

     @Before
     public void before() {
         System.out.println("BEFORE");
     }

     @Test
     public List testGetItems(@Param(name="itemId")Double itemId , @Param(name="itemType")String itemType) {
       
          //Actual test conditions here
           System.out.println("Run testGetItems");

     }

     @Test
     public void testUpdateItem(@Param(name="item") Item item) {
       
          //Actual test conditions here
          System.out.println("Run testUpdateItem");

     }

    @After
     public void after() {
         System.out.println("AFTER");
     }
}

The above example uses existing functionalities of EasyTest, like providing data in a test file, using CDI annotations to inject test beans. If you want to know more about the @TestConfigProvider and Dependency Injection in EasyTest you can see my previous blog post. If you want to know more about how to write Data Driven Tests in general using EasyTest, you can visit EasyTest’s WIKI Pages.

Now the above is an example of running a simple Data Driven Unit Test. All the above tests will run in Serial, one after the other. Assuming that each of the test method has two sets of test data specified in the getItemData.csv file, when the above test is run, we get the following on the console :

BEFORE
Run testGetItems
AFTER

BEFORE
Run testGetItems
AFTER

BEFORE
Run testUpdateItem
AFTER

BEFORE
Run testUpdateItem
AFTER

STEP 3:

Next, lets make the above tests run in Parallel. Simply include the @Parallel annotation at the class level and provide the number of threads you want to run. For the above test case we will run two threads.

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths="getItemData.csv")
@TestConfigProvider(TestConfigProvider.class)
@Parallel(threads=2)
public class ItemServiceTest {

     @Inject
     private ItemService itemService;

     @Before
     public void before() {
         System.out.println("BEFORE");
     }

     @Test
     public List testGetItems(@Param(name="itemId")Double itemId , @Param(name="itemType")String itemType) {
       
          //Actual test conditions here
           System.out.println("Run testGetItems");

     }

     @Test
     public void testUpdateItem(@Param(name="item") Item item) {
       
          //Actual test conditions here
          System.out.println("Run testUpdateItem");

     }

    @After
     public void after() {
         System.out.println("AFTER");
     }
}

Note the @Parallel annotation at the lass level.

When this test is run, the console output looks something like this(it may vary when you run the same test):

BEFORE
BEFORE
Run testGetItems
BEFORE
Run testUpdateItem
AFTER
Run testGetItems
BEFORE
AFTER
Run testUpdateItem
AFTER
AFTER

As you can see from the above console output, the tests are running alongside each other and thats why you see such a distributed console output.

Conclusion

We saw in the above example, how you can now run your tests in Parallel. Also when building your tests from build tools like Maven, the tests will run in Parallel. This is a big boost to scenarios where you have thousands of Unit Tests and they take minutes to run.

EasyTest is working on a command line/system parameter approach to enabling/disabling of Parallel feature. Stay tuned.
 

Reference: Run your Unit Tests in Parallel from our JCG partner Anuj Kumar at the JavaWorld Blog blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


seven × 2 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books