Writing a simple Junit 5 test

Carvia Tech | October 27, 2019 | 2 min read | 1 views | java junit


In this article we will write a simple JUNIT 5 based testcase, to get you familiar with syntax of Junit Jupiter framework.

Gradle setup

For non Spring Boot Projects, you need add Junit Jupiter dependencies.

build.gradle
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.5.2') (1)
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}
1 junit-jupiter transitively pulls in dependencies on junit-jupiter-api, junit-jupiter-params, and junit-jupiter-engine

For Spring Boot based projects (2.2.0 onwards), you need to add the following dependency.

build.gradle
plugins {
    id "java"
    id 'org.springframework.boot' version "2.2.0.RELEASE"
    id "io.spring.dependency-management" version "1.0.8.RELEASE"
}

dependencies {
    testImplementation('org.springframework.boot:spring-boot-starter-test') {   (1)
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}
1 spring-boot-starter-test transitively pulls in dependencies for Junit Jupiter framework. It also includes junit-vintage-engine dependency that help us run junit 4 based tests in parallel. You can exclude it if there are no junit 4 based tests in your application.

Latest version of junit-jupiter can be found on Maven Central

Writing JUnit 5 test

Few important points about Junit 5 framework:

  • Annotations reside in the org.junit.jupiter.api package

  • Assertions reside in org.junit.jupiter.api.Assertions

  • Assumptions reside in org.junit.jupiter.api.Assumptions

We will write a basic JUnit 5 testcase that tests Palindrome algorithm for correctness.

PalindromeTest.java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

import static org.junit.jupiter.api.Assertions.*;

class PalindromeTest {

    @Test
    @DisplayName("negative testcase for Palindrome")
    void isPalindromeNegative() {
        Palindrome checker = new Palindrome();
        assertFalse(checker.isPalindrome(123), "Number 123 is not a palindrome");
    }

    @Test
    void isPalindromePositive() {
        Palindrome checker = new Palindrome();
        assertTrue(checker.isPalindrome(626), "Number 626 is a palindrome");
    }
}

Spring Boot test using JUnit 5 Jupiter

To run Spring Boot based tests, we need to annotate the test class with @SpringBootTest. This will automatically configure TestRestTemplate and WebTestClient for use in test cases.

ApplicationTests.kt
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit.jupiter.SpringExtension

@SpringBootTest
@ExtendWith(SpringExtension::class) (1)
class ApplicationTests {

    @Test
    fun contextLoads() {

    }
}
1 @ExtendWith is not required if you are using Spring Boot 2.2.0.RELEASE onwards (as it is part of @SpringBootTest meta-annotation itself).

java junit:
  1. Migrating Spring Boot tests from Junit 4 to Junit 5
  2. Creating custom Tag in Junit5 based tests
  3. Parameterized Tests using JUnit 5
See all articles in java junit
Top articles in this category:
  1. SDET: JUnit interview questions for automation engineer
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. SDET: Rest Assured Interview Questions
  4. Junit 5 Platform Launcher API
  5. Parameterized Tests using JUnit 5
  6. Creating custom Tag in Junit5 based tests
  7. Migrating Spring Boot tests from Junit 4 to Junit 5



Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info