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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× three = 18



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close