dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.5.2') (1)
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
Writing a simple Junit 5 test
Upasana | October 27, 2019 | 2 min read | 96 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.
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.
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.
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.
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:
- Migrating Spring Boot tests from Junit 4 to Junit 5
- JUnit 5 Parameterized Tests
- Creating custom Tag in Junit5 based tests
Top articles in this category:
- JUnit 5 Parameterized Tests
- Migrating Spring Boot tests from Junit 4 to Junit 5
- Junit interview questions for SDET automation engineer
- Junit 5 Platform Launcher API
- Rest Assured API Testing Interview Questions
- 50 Java Interview Questions for SDET Automation Engineer
- Creating custom Tag in Junit5 based tests