Enterprise Java

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.

EasyTest_Repeat

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.

 

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button