Spring Boot Test if web server starts on a specific port

Below is a Spring Boot Test example to test whether the web server started on port 8181. P.S. Tested with Spring Boot 3 and JUnit 5. application.properties server.port = 8181 WebServerTests.java package com.mkyong; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class WebServerTests { @LocalServerPort private int port; @Test public …

Read more

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

Jersey and Jetty HTTP Server examples

This article shows how to start a Jetty HTTP Sever to run a JAX-RS or Eclipse Jersey application. Tested with Jersey 3.0.2 Jetty 11 Jackson 2.12.2 JUnit 5.4.0 (unit test) JSONassert 1.5.0 (unit test) Maven 3.8.3 Java 11 Tables of contents 1. Using Jersey with Jetty HTTP Server 2. Project Directory 3. Project dependencies 4. …

Read more

Spring Boot JobRunr examples

This article shows how to use Spring Boot to create REST endpoints to run the background jobs managed by the JobRunr. Tested with: Spring Boot 2.3.12.RELEASE JobRunr 3.1.2 Maven 3 1. Directory Structure. A standard Maven project structure. 2. Project Dependencies. We need a single jobrunr-spring-boot-starter to integrate Spring Boot web and JobRunr. pom.xml <!– …

Read more

JUnit 5 Tutorials

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage P.S JUnit 5 requires Java 8 (or higher) at runtime 1. JUnit 5 + Maven See this full JUnit 5 + Maven examples. pom.xml <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M3</version> </plugin> </plugins> </build> P.S The maven-surefire-plugin must be …

Read more

JUnit 5 + AssertJ examples

In this article, we will show you how to write test assertions with AssertJ. P.S Tested with JUnit 5.5.2 and AssertJ 3.14.0 pom.xml <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.14.0</version> <scope>test</scope> </dependency> 1. JUnit 5 assertions to AssertJ It’s easy to convert JUnit 5 assertions to AssetJ, see the following syntax: JUnit …

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 Timeouts Examples

In JUnit 5, we can use @Timeout to fail a test if the execution time exceeds a given duration. P.S Tested with JUnit 5.5.2 1. @Timeout TimeOutExample1.java package com.mkyong.timeout; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import java.util.concurrent.TimeUnit; public class TimeOutExample1 { // timed out after 5 seconds @BeforeEach @Timeout(5) void setUpDB() throws InterruptedException { //TimeUnit.SECONDS.sleep(10); …

Read more

JUnit 5 Expected Exception

In JUnit 5, we can use assertThrows to assert an exception is thrown. P.S Tested with JUnit 5.5.2 1. Unchecked Exception 1.1 JUnit example of catching a runtime exception. ExceptionExample1.java package com.mkyong.assertions; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ExceptionExample1 { @Test void test_exception() { Exception exception = assertThrows( ArithmeticException.class, () -> divide(1, 0)); assertEquals("/ …

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

JUnit 5 ConsoleLauncher examples

This article shows you how to use the JUnit 5 ConsoleLauncher to run tests from a command line. Tested with JUnit 5.5.2 junit-platform-console-standalone 1.5.2 1. Download JAR To run tests from the command line, we can download the junit-platform-console-standalone.jar from Maven central repository manually. P.S This example is using version 1.5.2. https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.5.2/ 2. Usages Usually, …

Read more

JUnit 5 Repeated Tests

This article shows you how to use the JUnit 5 @RepeatedTest to repeat a test a specified number of times. P.S Tested with JUnit 5.5.2 1. @RepeatedTest 1.1 The @RepeatedTest test method is just like a regular @Test method, the same life cycle. RepeatedSample1Test.java package com.mkyong.repeated; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.assertEquals; public class RepeatedSample1Test { …

Read more

JUnit 5 Nested Tests

This article shows you how to use the JUnit 5 @Nested annotation to group tests together. P.S Tested with JUnit 5.5.2 Why nested tests? It’s optional to create nested tests. Still, it helps to create hierarchical contexts to structure the related unit tests together; in short, it helps to keep the tests clean and readable. …

Read more

JUnit 5 Test Execution Order

This article shows you how to control the JUnit 5 test execution order via the following MethodOrderer classes: Alphanumeric OrderAnnotation Random Custom Order P.S Tested with JUnit 5.5.2 1. Alphanumeric 1.1 It sorts test methods alphanumerically. MethodAlphanumericTest.java package com.mkyong.order; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import static org.junit.jupiter.api.Assertions.assertEquals; @TestMethodOrder(MethodOrderer.Alphanumeric.class) public class MethodAlphanumericTest { @Test void …

Read more

JUnit 5 Tagging and Filtering, @Tag examples

This article shows you how to use the JUnit 5 tagging and filtering via the @Tag annotation. Tested with JUnit 5.5.2 Maven 3.6.0 Gradle 5.6.2 1. @Tag A simple tagging for demo. TagMethodTest.java package com.mkyong.tags; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @Tag("branch-20") public class TagMethodTest { @Test @Tag("feature-168") void test1Plus1() { assertEquals(2, 1 + …

Read more

JUnit 5 – How to disable tests?

JUnit 5 @Disabled example to disable tests on the entire test class or individual test methods. P.S Tested with JUnit 5.5.2 Note You can also disable tests based on conditions. 1. @Disabled on Method 1.1 The test method testCustomerServiceGet is disabled. DisabledMethodTest.java package com.mkyong.disable; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class DisabledMethodTest { …

Read more

JUnit 5 Conditional Test Examples

This article shows you how to use JUnit 5 to enable or disable tests based on conditions. P.S Tested with JUnit 5.5.2 1. Operating System 1.1 Enabled or disabled tests based on a particular operating system via @EnabledOnOs and @DisabledOnOs annotations. OperatingSystemTest.java package com.mkyong.conditional; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; public class OperatingSystemTest …

Read more

JUnit 5 + Gradle examples

This article shows you how to add JUnit 5 in a Gradle project. Technologies used: Gradle 5.4.1 Java 8 JUnit 5.5.2 1. Gradle + JUnit 5 1. Add the JUni 5 jupiter engine, and define the useJUnitPlatform() like the following: gradle.build plugins { id ‘java’ id ‘eclipse’ // optional, for Eclipse project id ‘idea’ // …

Read more

JUnit 5 + Maven examples

This article shows you how to add JUnit 5 in a Maven project, nothing special, just add the JUnit 5 junit-jupiter-engine library and make sure the maven-surefire-plugin is at least version 2.22.0 Technologies used: Maven 3.6 Java 8 JUnit 5.5.2 1. Maven + JUnit 5 1. Add the JUni 5 jupiter engine. pom.xml <dependency> <groupId>org.junit.jupiter</groupId> …

Read more

Spring Boot + JUnit 5 + Mockito

In this article, we will show you how to do Spring Boot 2 integration test with JUnit 5, and also Mockito. Spring Boot 2.1.2.RELEASE JUnit 5 Mockito 2 Maven 3 In short, exclude junit4 from spring-boot-starter-test, and include the JUnit 5 jupiter engine manually, done. Let see the following Spring boot MVC web application, and …

Read more

Spring Boot WebFlux + Server-sent events example

In this article, we will show you how to develop a reactive web application, using Server-sent events Spring Boot 2.1.2.RELEASE Spring WebFlux 5.1.4.RELEASE Thymeleaf 3.0.11.RELEASE JUnit 5.3.2 Maven 3 In Spring, returns JSON and header MediaType.TEXT_EVENT_STREAM_VALUE @RestController public class CommentController { @GetMapping(path = "/comment/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<Comment> feed() { //… } } In …

Read more

Maven – How to create a multi module project

In this tutorial, we will show you how to use Maven to manage a Multi-module project containing four modules : Password module – Interface only. Password md5 module – Password module implementation, MD5 password hashing. Password sha module – Password module implementation, SHA password hashing. Web module – A simple MVC web app to hash …

Read more

Maven test failed on Spring @Autowired and jUnit 5

Review the following jUnit 5 examples to test a Spring 5 MVC web application. TestWelcome.java package com.mkyong.web; import com.mkyong.web.config.SpringConfig; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @ExtendWith(SpringExtension.class) @WebAppConfiguration @ContextConfiguration(classes = …

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

Jersey and JSON examples (Jackson)

This article shows how to return a JSON response in the Jersey application, using Jackson 2.x. Tested with Jersey 3.0.2 Grizzly 3 HTTP Server Jackson 2.12.2 Java 11 Maven JUnit 5 and JSONassert 1.5 (Unit Test) Table of contents 1. Jackson as the JSON provider in Jersey 2. Project Directory 3. Project dependencies 4. Jersey …

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