Unit-testing in Java

author-image
CIOL Bureau
Updated On
New Update

BANGALORE, INDIA: Unit testing is a practice that developers use to test their code modules in isolation to make sure that each module does what it's supposed to. JUnit is an Open Source framework which helps in unit testing, specifically coded on the Java platform. Developers just have to determine the test cases and create JUnit tests for their code modules.

Advertisment

In a typical setup, developers don't prefer this added burden of unit-testing on their shoulders, as they feel that this should be done by the testing division. They often overlook the benefits of doing unit-testing at developmental stage, because it requires too much work. This used to make it extremely difficult to convince developers to do unit-testing at their end.

Direct Hit!
Applies To: Java developers
USP: JUnit generation and code-rule enforcement
Price: $50,000 per year
Primary Link: www.agitar.com/
products/downloads/
Keywords:
AgitarOne

Now, there's a tool that has been introduced by Agitar, called Agitar One, which creates JUnit tests automatically for code modules and also prepares the test cases for them. This significantly reduces the time required for unit testing, enabling an organization to convince its developers to take up the job.

Advertisment


AgitarOne steps in

As we mentioned, AgitarOne is a unit-testing software for enterprises who are engaged in Java-based development. Project managers can use it at developmental stage so that their developers can implement unit-testing.

It is a server-based model and can execute through the code modules to test how the given code will perform under normal as well as exceptional conditions. AgitarOne relieves the developer by generating several test-cases for code modules, even for those code patterns which developers find tedious to create the JUnit tests for and normally skip when doing the task, manually.

Once a module is put under test creation, AgitarOne reads through the code, creates the test on-the-fly and generates the report based on the results, observing which lines of code were executed and which lines were excluded. Any anomalies in the code can easily be caught from these reports.

Advertisment

Agitar's dashboard gives an overall summary of the project and detailed reports on each class and its risk-analysis

The test values that AgitarOne uses are based on random generation of values for variables and functions. When any test fails, AgitarOne reports the sequence of functions and values passed to them so that a developer may trace through the code to evaluate the reason why a particular code couldn't handle the value, as per expectation. Like this, quality can be ensured for a code module right from the development process.

Advertisment
 

Installation and setup

AgitarOne is based on Eclipse IDE and it comes packaged with it. Alternatively, it can also be installed separately as a plug-in to an existing Eclipse IDE. In the packaged version, Tomcat server is automatically configured for AgitarOne and the client machines can access AgitarOne's Dashboard remotely.

Once the server and clients have been configured, it's just a few clicks of process for developers to automate the unit-testing process for their code modules.

Advertisment

The developer has to select his project or a class-file and through Agitar's drop-down menu he can select to create tests, agitate the code, run the code-rule or generate the Dashboard result of the project.


AgitarOne in action

The software agitates on a given code module to check for what that code was intended to do and what it actually does. Agitation can be described as a process of creating multiple instances of classes, and calling all methods of those classes with a wide array of input data and observing the results thus generated.

This input data can be randomly generated by AgitarOne or can be manually passed by the developer through factories (user-defined subclasses). Then it's up to the developer to check which observation forwarded by AgitarOne is not matching the desired behavior and the reason for it.

Advertisment

For experiencing this agitation process, let us take a sample project provided with AgitarOne. We will try agitating Product class and see the observations presented by AgitarOne, so that we are able to assert on a few of them to validate code behavior.

In the Product class, we have validation checks for name and price fields. Where name can't be more than 20 characters and price field can have a value between 0 and 1000.

To start the agitation process on Product class, select Product.java and click on Agitate toolbar button or press Ctrl+F9. Now, in the editor window for Product.java, we can see in the Code Coverage style the number of times a particular line of code has been executed. The red indicates that some lines have not been touched by control flow. In AgitarOne Server view, status and coverage (indicates what percent of the code lines have been executed) is displayed.

Advertisment

 

The Observations view indicates where the problems may be lying with the code module, in our case the Product class. Notice the following observation listing for the Product class.


0 <= this.getName().length() <= 46

As we had defined that a name can't be more than 20 characters long, in this observation we can see that AgitarOne was able to pass more than 20 characters long values to the field 'name'. This means a validation for that is not being executed or is being ignored by the program control during execution.

Now the developer can directly presume that the validateName() method of the code is not being called when setName() method passes the value for name field. By changing the setName method to look as following code snippet we can eliminate the error.


public void setName(String name) throws IllegalArgumentException {
validateName(name);
this.name = name;
}

When we again agitate the Product class, under the Observations view, we see a modified observation listed. The observation for getName() has also been modified.

Similarly, the developer can make assessment for other observations. He can assert an observation to hold true for a situation and can again run agitation to check the code behavior. This way a developer, who is well familiar with his code, can at developmental stage only eliminate possible redundant errors, by using AgitarOne.

The functionality of Dashboard is an added advantage, both for a developer and the project leader. As in Dashboard, the whole coverage percentage of the projects' classes, their progress and the highest risk classes can be viewed.

Upon agitation the coverage style is represented along the editor code lines and Observations view shows the various observations from the agitation result


Conclusion

With the automation of unit-testing, a developer with the help of AgitarOne can implement quality code creation, right from developmental stage onwards. Hence, enterprises will benefit with reduced time of testing and developers will be able to debug their code and improve productivity, significantly.

tech-news