Site icon TestingDocs.com

JUnit Unit Testing Framework

What is JUnit?

JUnit is a simple, open-source Unit Testing framework tool to write and run repeatable unit tests. Also, it is an instance of the xUnit architecture for unit testing frameworks. It is mostly used as a Unit testing tool by most developers.

JUnit main features are as below:

Test runner for running tests as shown in the below picture.

 

 

 

JUnit 4 Maven dependency

If your build tool is Maven, then add a dependency to JUnit to pom.xml. Replace the minor version to the latest stable version.

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.xx</version>
  <scope>test</scope>
</dependency>

 

JUnit 4 Annotations:

Annotations mark the tests as test cases ex: with @Test. The most common JUnit 4 annotations are:

@Test
@Before
@After

A simple JUnit test:

The @Test annotation tells JUnit that the method to which it is attached can be run as a test case. Also, to run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. In addition, any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

 

@Test
public void testMethod()
{
assertEquals(1, 1);
}

 

https://www.testingdocs.com/anatomy-of-a-junit-test/

In conclusion, the most recommended tool for writing/executing Java unit tests is JUnit. JUnit 4 is a major API revision release  ( However, a new version JUnit5 is around the corner).

 

JUnit Tutorial on this website can be found at:

https://www.testingdocs.com/junit-tutorial/

More information on JUnit official website:

https://junit.org

Exit mobile version