About Me

My Photo
A Sun certified Java professional with time proven experience in architecture and designing of mid/large scale Java/JEE based applications. Creator of the EasyTest Framework.A lot of experience with technologies such as Spring framework, Hibernate , JPA, Java4-6, REST, SOA , YUI , JUnit , Cloud Foundry PaaS and other technologies.

Friday, October 11, 2013

Write effective Load Tests using JUnit and Repeat annotation

EasyTest recently introduced a new set of annotations that would help its users write effective test cases. The two main annotations that found their way into EasyTest are :

  • Repeat
  • Duration
Today we will discuss about the Repeat annotation.

A new method level annotation Repeat has been added to the EasyTest Framework. This annotation can be used to repeat the same test multiple times. This annotation is useful in scenarios where you may quickly want to load test your application. Here is how this annotation can be used.  

public class TestJSON {

        @Test
        @Repeat(times=20)
        public Item testJSONObject(@Param(name='itemId')String itemId, @Param(name='itemType')String itemType) {
           Item result = testSubject.findItem(itemId)
             Assert.notNull(result);
             return result;
        }

Notice the Repeat annotation at the method level. When EasyTest sees this annotation, it creates "n" different instances of the test method, where "n" is defined by the "times" attribute of the Repeat annotation. In the above case, EasyTest will create 20 unique instances of the above test method.

There is also a System Property test.repeatCount that can be used while running tests from command line. When this property is set, EasyTest simply creates "n" instances of each test defined in the test class, where "n" is defined by the value of the above System Property. System Property takes precedence over Repeat annotation. It means that if both annotation and system property is present, then System Property's value will be used.

In case you have 3 set of input test data defined for a given test method which has a Repeat annotation with times = 20, the test will be run 20 times for each input test data. Thus total times that the test will run is 3 X 20 = 60.

This is a really quick and effective way to load test your application without any specific setup. Be aware that this alone cannot give you a true picture of your environment, but it can definitely be a good starting point.

When run on the IDE, each test case name will be appended with "_n" where n ranges from 0 till (times - 1), where times is the number specified in the Repeat annotation. Here is a screen shot for reference.


Thus as you can see, EasyTest provides the users with an quick and effective way of converting their existing unit tests into a load test with just the use of a single annotation.

In the next blog post we will discuss Duration annotation.
For a quick introduction to EasyTest look at the Quick Start Guide to EasyTest.
For a detail on what EasyTest provides look at the README file on EasyTest's Github page.
For more details look at one of the EasyTest WIKI pages.

No comments: