How to pass a null value in JUnit 5 parameterized tests

How to pass a null for the below parameterized tests? @ParameterizedTest(name = "#{index} – Run test with args={0}") @ValueSource(strings = {"", " "}) // @ValueSource(strings = {"", " ", null}) // compile error void testBlankIncludeNull(String input) { assertTrue(StringUtils.isBlank(input)); } 1. Solution – @NullSource Since JUnit 5.4 and above, we can use the @NullSource to pass …

Read more

JUnit 5 Display Names

In JUnit 5, we can use @DisplayName to declare custom display names for test classes and test methods. P.S Tested with JUnit 5.5.2 1. @DisplayName 1.1 Default name of test classes and methods. DisplayNameTest.java package com.mkyong.display; import org.junit.jupiter.api.Test; public class DisplayNameTest { @Test void test_spaces_ok() { } @Test void test_spaces_fail() { } } Output +– …

Read more

JUnit 5 Parameterized Tests

This article shows you how to run a test multiple times with different arguments, so-called ‘Parameterized Tests’, let see the following ways to provide arguments to the test: @ValueSource @EnumSource @MethodSource @CsvSource @CsvFileSource @ArgumentsSource We need junit-jupiter-params to support parameterized tests. pom.xml <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency> <!– Parameterized Tests –> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> …

Read more

Gradle – How to display test result in the console

By default, gradle test displays only the test summary. Terminal % gradle test BUILD SUCCESSFUL in 4s 6 actionable tasks: 3 executed, 3 up-to-date P.S Gradle test generates the detailed tests’ result at the build/reports/tests/test/index.html page. P.S Tested with Gradle 6.7.1 1. Display test result in the console. 1.1 We add the testLogging events to …

Read more

TestNG parameter testing example

Yet another TestNG parameter testing example, with @DataProvider. 1. CharUtil Class Let’s say, a class to convert a character to ASCII or vice verse, how can you unit test it with TestNG? CharUtils.java package com.mkyong.testng.examples.parameter; /** * Character Utility class * * @author mkyong * */ public class CharUtils { /** * Convert the characters …

Read more

Java IP Address (IPv4) regex examples

This article focus on how to validate an IP address (IPv4) using regex and Apache Commons Validator. Here is the summary. IPv4 regex explanation. Java IPv4 validator, using regex. Java IPv4 validator, using commons-validator-1.7 JUnit 5 unit tests for the above IPv4 validators. IPv4 regex final version. ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.(?!$)|$)){4}$ # Explanation ( [0-9] # 0-9 | …

Read more

JUnit – Parameterized Test

In JUnit, you can pass the parameters into the unit test method via the following methods : Constructor Fields injection via @Parameter P.S Tested with JUnit 4.12 1. MatchUtils – Test with multiple parameters A simple add operation. MathUtils.java package com.mkyong.examples; public class MathUtils { public static int add(int a, int b) { return a …

Read more

TestNG – Parameter Test (XML and @DataProvider)

In this tutorial, we will show you how to pass parameters into a @Test method, via XML @Parameters or @DataProvider. 1. Passing Parameters with XML In this example, the properties filename is passing from testng.xml, and inject into the method via @Parameters. TestParameterXML.java package com.mkyong.testng.examples.parameter; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; …

Read more