JUnit
3 min readMar 5, 2021
Introduction
- During development the first thing we do is develop, run and test. And this cycle repeats until the release or developer is satisfied.
- This testing the same thing again and again is repetitive and developer could miss a test. To avoid this many programmers write simple programs with main to test their code.
- There are different types of tests also as many of you know. Like Unit testing, integration testing, system testing… We will be concentrating on Unit testing (A unit test examines the behavior of a distinct unit of work.).
- Unit testing is not intended to test the complete functionality of an application.
- JUnit is one such framework (It is reusable common app or structure which could be incorporated as per user’s requirements) which will help in writing unit tests in java. Junit is java based Unit Testing Framework which we use to test our Application.
- We generally write Junits to test each individual piece of our application.
Advantages of writing JUnit test/unit tests
- It proves that a piece of code works passing a series of tests.
- Give the confidence as tests are the first clients to the code written.
- Allow greater test coverage than functional tests. If we test functionality wise we might not be able to test all the scenarios like boundary conditions, all conditions flows, exception conditions… Unit tests will help here and in turn give better coverage.
- Increase team productivity . Unit tests allow delivering quality code without having to wait for other components to be complete.
- Detect regressions and limit the need for debugging . Unit tests can help developers identify bugs easy without debugging as they are at method level.
- Give us the confidence to refactor and, in general, make changes
- Once we refactor our code or make any changes and if unit tests go through then it gives the confidence that code is fine.
- Improve implementation. Unit tests are the first clients for the code written and if it is getting difficult to write unit tests or unit tests are big then code needs to be looked at.
- Document expected behavior. Unit tests are working examples of the API to be tested and provide better documentation purpose.
- Enable code coverage and other metrics
What is JUnit?
JUnit is made up of following core objects:
- Test: A method with a @Test annotation defines a test.
- Test Case: A class that contains one or more tests represented by methods annotated with @Test
- Assertions: Are used to validate the tests. Following are few examples
- assertEquals
- assertSame
- assertNotNull
- assertTrue
- assertFalse
How to define a JUnit
- Create a test class with a specific naming convention including the class under test. For example, to test Hello class, create a class HelloTest. Convention is like <Class Under Test>Test.java.
- Import static org.junit.Assert.* for assertions and other objects like Test, Before, After, BeforeClass and AfterClass as needed by test.
- Before methods get executed before every test invocation in the same class. After methods get executed after every test execution in the same class. Any methods could be annotated with these @Before and @After. They are used to setup/teardown things which are required for things to run in the test.
- But for beforeclass and afterclass they are executed once per class.
- Create a method which is used to test a method in the class to be tested. This should follow a convetion like test<MethodUnderTest>, should be declared public with void return type and no parameters. It should be annotated with @Test show that it is recognized as a test method.
- The normal testing flow is such that we begin with inserting test data then we follow up with actual testing of Business Components.
Next Steps :-