Junit 5 – A dive into Junit testing

Introduction:

Junit in general is a framework that helps developers to implement UNIT TESTS for their code where Junit 5 is the framework which is still under development with alpha release already out. However, native tool support is not ideal, yet but there are early access implementations and workarounds to get everything running.

Junit 5 is composed of different modules namely

      Junit Platform + Junit Jupiter + Junit Vintage

  1. Junit Platform:

It is responsible for launching testing frameworks on JVM. It defines a stable and powerful interface between Junit and build tools such as Maven, Ant etc. and IDEs (Eclipse, Intellij) etc.

It also defines a TestEngine API for developing testing frameworks that runs on unit platform. 3rd party libraries such as Spock Framework can be plugged into Junit platform

   2. Junit Jupiter:

It includes support for new annotations such as @Tag, @DisplayName, @Nested etc. Definitions about the new annotations:

@Tag: is used to filter tests.

@DisplayName: is used to provide  name for a test class or a test method.

@Nested: is used to indicate that annotated class is nested and non static class.

@Disable: is used to disable a test class or test method.  This is called @Ignore in Junit 4

@BeforeEach: is used to indicate that annotated test method will be executed before each test method. This is called @Before in Junit 4.

@AfterEach: is used to indicate that annotated test method will be executed after each test method. This is called @After in Junit 4.

@BeforeAll: indicates that annotated method will be executed before all the test methods in the class. This is called @BeforeClass in Junit 4

@AfterAll: indicates that annotated method will be executed after all the test methods in the class. This is called @AfterClass in Junit 4.

  3. Junit Vintage:

It is used to support the running of Junit 3 and Junit 4 tests.

Example:

Imagine a class which performs addition and subtraction:

public class MathResult {

int a;

int b;

public int getAddition(int a, int b) {

return a+b;

}

public int getSubtraction(int a, int b) {

return a-b;

}

}

A Junit 5 based Test class would look like

@RunWith(JUnitPlatform.class)

public class TestMathResult {

  @Test

  @Tags({@Tag("addition"), @Tag("subtract"), @Tag("MathResult")})

  @DisplayName("Testing Math Result class")

public void test additionAndSubtraction() {

MathResult mr = new MathResult();

assertAll( () -> assertEquals(11, mr.getAddition(5,6),

() -> assertEquals(5, mr.getSubtraction(11, 6),

() -> assertEquals(15, mr.getAddition(11, 4)) ;

}

 

 

Until, a stable version of Junit 5 is released, here below are the maven dependencies needed:

  • The api for writing Junit 5 tests is contained in:

Group ID: org.junit.jupiter
Artifact ID: junit-jupiter-api
Version: 5.0.0-M3

  • The dependency for running tests is the JunitPlatform which would run new tests as a part of Junit4 run.

Group ID: org.junit.platform
Artifact ID: junit-platform-runner
Version: 1.0.0-M3
Scope: test

  • The dependency that actually calls the test is the Junit Jupiter engine. This is called by JunitPlatform

Group ID: org.junit.jupiter
Artifact ID: junit-jupiter-engine
Version: 5.0.0-M3
Scope: test runtime

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s