Insights from Our Experts

Blog image

Journey to JUnit!

Author Image

Jyothi V,Associate QA Engineer

Introduction

When we are hooked with the development of a project, there arises a basic need for it to be unit tested. This would assure that the coded script executes its aimed functionalities. For such a reason, JUnit is a rescuer. JUnit will lend a helping hand to develop superior, robust tests for your projects. JUnit5  will be the offspring of the extant JUnit.

JUnit is mainly used for unit testing. Erich Gamma and Kent Beck initially developed it. It is an open source framework for Java programming language. JUnit is used by Java developers to write and run repeatable tests. It is an instance of xUnit architecture. The org.JUnit package consists of many interfaces and classes for JUnit testing. For example, they have: Test, Assert, After, Before, and many more. JUnit tests can be run automatically and they check their own results and provide immediate feedback. There is no need to manually comb through a report of test results.

We can say JUnit is a library which makes it easy for testers to write and run java code with assertions. JUnit test is simply a method in a class annotated with @Test. It is with the help of these annotations that we write test cases. Or, we can say annotations help to distinguish test cases from usual coding.

How JUnit helps testers?

JUnit is used not only for unit testing but also for other types of testing. JUnit can be used as a test runner for various kinds of testing like Integration Testing, System Testing and Interactive Exploratory. When we write test scripts with the help of JUnit, we require web-drivers to initiate testing the applications. While the test scripts run, it interacts with the application to be tested and delivers the test results.

Significant Classes in JUnit

  • Test - It is a class that allows writing test scripts.
  • Suite - A class which runs all the unit test cases defined in @SuiteClasses annotation
  • Assert - It is a class that provides a lot of assert methods which can be used to throw an exception if the given proposition fails.
  • Runner - It runs a unit test/suite and notifies RunNotifier of the significant event.
  • RunListener - It is a class that listens to events triggered by Runner (For example, Test Begin, Test End)
  • Result - It is a class that collects and summarizes all information from multiple tests.

 

Consider a simple example of JUnit. Here, the ‘UserSort’ is a class which will compare usernames. Each user details are received through the getUsername method. This comparison is done with the help of assertEquals method. The assertEquals method checks whether the usernames are equal or not. If they are not, an exception error will be thrown. Else result will be a success. Generally, @ is used to represent the annotation in JUnit. Here,  @Test method helps to create a test case for sorting the usernames using SortedSet method.

Conclusion

At first, it might be difficult to write test cases for your applications. But when we get a hang of it, it will be a great use of. JUnit has been important in test-driven development. JUnit accelerate programming speed and increase the quality of the code.