Unit Testing With JUnit 5 - Course Guide
Unit Testing With JUnit 5 - Course Guide
Unit Testing With JUnit 5 - Course Guide
Unit T
JUnit 5
1. Congratulations
2. About in28Minutes
3. Troubleshooting Guide
4. Getting Started
7. References
on APIs, Web Services and Microservices with Spring, Spring Boot and
Spring Cloud.
About in28Minutes
What's that?
You need to get insight into the in28Minutes world to answer that.
What do we love?
Hands-on
Step By Step
Code on Github
Interview Guides
Building a connect
Socially Conscious
We love what we do
Troubleshooting Guide
We love all our 150,000 learners. We want to help you in every way
possible.
This 50 page troubleshooting guide and faq is our way of thanking you for
Recommended Versions
Installation
Video : https://www.youtube.com/playlist?
list=PLBBog2r6uMCSmMVTW_QmDLyASBvovyAO3
PDF
: https://github.com/in28minutes/SpringIn28Minutes/blob/master/InstallationGuid
e-JavaEclipseAndMaven_v2.pdf
More Details : https://github.com/in28minutes/getting-started-in-5-steps
Troubleshooting
A 50 page troubleshooting guide with more than 200 Errors and Questions
answered
Unit Testing with JUnit 5 -
Course Overview
Github Page :
https://github.com/in28minutes/spring-unit-testing-with-junit-and-
mockito/blob/master/junit-5-course-guide.md
Overview
JUnit is most popular Java Unit Testing Framework. The new version of JUnit - Junit 5
or Jupiter is even more special.
In this course, we look into the important features of JUnit 5. We understand the need
for unit testing and learn how to write great unit tests with JUnit 5.
Exercises
Step 04 - First Unit Test with JUnit - String length() method
Write unit test for Math.min and Math.max methods!
package com.in28minutes.junit5;
import static
org.junit.jupiter.api.Assertions.assertArrayEquals;
import static
org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static
org.junit.jupiter.api.Assertions.assertNotNull;
import static
org.junit.jupiter.api.Assertions.assertThrows;
import static
org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
class StringTest {
@BeforeAll
static void beforeAll() {
System.out.println("Initialize connection
to database");
}
@AfterAll
static void afterAll() {
System.out.println("Close connection to
database");
}
@BeforeEach // @Before
void beforeEach(TestInfo info) {
System.out.println("Initialize Test Data
for " + info.getDisplayName());
}
@AfterEach // @After
void afterEach(TestInfo info) {
System.out.println("Clean up Test Data for
" + info.getDisplayName());
}
@Test
@Disabled // @Ignored
void length_basic()
{
int actualLength = "ABCD".length();
int expectedLength = 4;
assertEquals(expectedLength,
actualLength);
// Assert length == 4
// Write test code
// Invoke method square(4) => CUT
// Checks in place - 16 => Assertions
}
@Test
void length_greater_than_zero() {
assertTrue("ABCD".length() > 0);
assertTrue("ABC".length() > 0);
assertTrue("A".length() > 0);
assertTrue("DEF".length() > 0);
}
@ParameterizedTest
@ValueSource(strings = { "ABCD", "ABC", "A", "DEF"
})
void
length_greater_than_zero_using_parameterized_test(String
str) {
assertTrue(str.length() > 0);
}
@Test
@DisplayName("When length is null, throw an
exception")
void length_exception() {
assertThrows(NullPointerException.class,
() -> {
str.length();
}
);
}
@Test
@Disabled
void performanceTest() {
assertTimeout(Duration.ofSeconds(5), () ->
{
for (int i = 0; i <= 1000000; i++)
{
int j = i;
System.out.println(j);
}
);
@Test
void toUpperCase_basic() {
String str = "abcd";
String result = str.toUpperCase();
assertNotNull(result);
// assertNull(result);
assertEquals("ABCD", result);
}
@Test
@RepeatedTest(1)
void contains_basic() {
assertFalse("abcdefgh".contains("ijk"));
}
@Test
void split_basic() {
String str = "abc def ghi";
String actualResult[] = str.split(" ");
String[] expectedResult = new String[] {
"abc", "def", "ghi" };
assertArrayEquals(expectedResult,
actualResult);
}
@Nested
@DisplayName("For an empty String")
class EmptyStringTests {
@BeforeEach
void setToEmpty() {
str = "";
@Test
@DisplayName("length should be zero")
void lengthIsZero() {
assertEquals(0,str.length());
}
@Test
@DisplayName("upper case is empty")
void uppercaseIsEmpty() {
assertEquals("",str.toUpperCase());
}
@Nested
@DisplayName("For large strings")
class LargeStringTests {
@BeforeEach
void setToALargeString() {
str =
"Afsjdjfljsadfkljsadlkfjlajbvjcxzbnhrewu";
@Test
void test() {
}
}
References
References
Intellij
https://www.jetbrains.com/help/idea/configuring-testing-libraries.html
https://blog.jetbrains.com/idea/2016/08/using-junit-5-in-intellij-idea/