Home » Java » Enterprise Java » Write effective Load Tests using JUnit and Repeat annotation

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.

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.

 

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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Leave a Reply

Your email address will not be published. Required fields are marked *

*


six + = 9

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close