↓ Skip to main content
Java

JUNIT5: Learning how to write the code the right way!

·2 mins

JUnit was developed by Kent Beck and Erich Gamma. Its first version was released in 1997.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage — https://junit.org/junit5/docs/current/user-guide/

It is powerful testing framework, with many modules. These modules are part of three sub-projects

  • Junit Platform: Serves as a foundation for launching testing frameworks on the JVM.
  • Junit Jupiter: It is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5.
  • Junit Vintage: Provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

Let’s start a basic project structure

mvn archetype:generate -DgroupId=samarthya.me -DartifactId=learnunit -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=true
Terminal output of mvn archetype:generate creating the learnunit project: BUILD SUCCESS
In previous JUnit versions, there was limited/no support to allow parameters in test constructors or methods. One of the major changes in JUnit 5 Jupiter was that both test constructors and test methods are now allowed to have parameters.

POM: JUNIT4 Support
#

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>samarthya.me</groupId>
  <artifactId>learnunit</artifactId>
  <packaging>jar</packaging>
  <version>v1.0</version>
  <name>learnunit</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Adding Libraries : JUNIT5
#

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>samarthya.me</groupId>
  <artifactId>learnunit</artifactId>
  <packaging>jar</packaging>
  <version>v1.0</version>
  <name>learnunit</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.7.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.7.2</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <version>5.7.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

JUNIT
#

JUNIT4: Test
#

package samarthya.me;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        System.out.println("JUNIT4: Test");
        assertTrue( true );
    }
}

JUNIT5: Test case
#

I will be using the @Test annotation that is from the package org.junit.jupiter.api.

package samarthya.me;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

class AppTest5 {

    @Test
    void test() {
        System.out.println("JUNIT5: Test");
        assertTrue(true);
    }    
}

Not much is there, but you can execute the suite and see the output. I am using Visual Studio Code and launching test gives the result as below.

VS Code Testing panel listing the learn-junit project with AppTest and AppTest5
VS Code debug console showing the output JUNIT5: Test and JUNIT4: Test

You can try some failures as below too..

Test Failure
Test Failure

SampleFunction
#

A trivial class with one sample function willReturnOne() that return one.

package samarthya.me;

public class SampleFunctions {
    public int willReturnOne() {
        return 1;
    }
}
SampleFunctionsTest class in VS Code with test_functionOne passing

Help
#

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Java Stream: Examples

·3 mins
Streams are convenient way to reduce the line of codes, but an understanding them is equally challenging. I write this blog, after realising a desperate attempt at solving a trivial problem just took overwhelming time. (Could be that I am getting old, but never late to learn things again)

Smallest Integer

·1 min
Code available here. The ask was to identify the smallest possible integer (> 0) in the array of integers. Example # 1. [-1,2] -> 1 2. [-1,-2] -> 1 3. [1,3] -> 2 I was initially thinking of using sets and then finding out the missing one, but then reaized it is simpler and I do not need sets

Fortune v1.0

·4 mins
In one of recent discussions with Mr. X I was asked about my approach to solving the Linux cookie problem A random quote Repository will be a text file Each quote will be segregated by a '%' Can be multiline. I always believe in incremental steps so here is what I could think of