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 Tutorials

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 Conditional Test Examples

In Spring Boot, we can create a @TestConfiguration class to initialize some beans for testing class only. P.S Tested with Spring Boot 2 1. @TestConfiguration + @Import This @TestConfiguration class will not pick up by the component scanning, we need to import it manually. TestConfig.java package com.mkyong; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import java.time.Duration; […]

Read more Spring Boot – How to init a Bean for testing?

In Java, there is no ready make API to compare two java.util.Set 1. Solution Here’s my implementation, combine check size + containsAll : SetUtils.java package com.mkyong.core.utils; import java.util.Set; public class SetUtils { public static boolean equals(Set<?> set1, Set<?> set2){ if(set1 == null || set2 ==null){ return false; } if(set1.size()!=set2.size()){ return false; } return set1.containsAll(set2); } […]

Read more Java – How to compare two Sets

In this tutorial, we will show you how to test the Spring DI components with JUnit frameworks. Technologies used : JUnit 4.12 Hamcrest 1.3 Spring 4.3.0.RELEASE Maven 1. Project Dependencies To integrate Spring with JUnit, you need spring-test.jar pom.xml <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> […]

Read more JUnit + Spring integration example

In JUnit, you can organize the test cases into different categories, and run those categorized test cases with @Categories.ExcludeCategory or @Categories.IncludeCategory Note This @Categories annotation is available since JUnit 4.12 1. Category = Marker Interface In JUnit, you need to create marker interfaces to represent the categories: PerformanceTests.java package com.mkyong.category; //category marker interface public interface […]

Read more JUnit – Categories Test

Includes hamcrest-library and test the class property and its value with hasProperty() : P.S Tested with JUnit 4.12 and hamcrest-library 1.3 ClassPropertyTest.java package com.mkyong; import org.junit.Test; import java.util.Arrays; import java.util.List; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.hasProperty; import static org.junit.Assert.assertThat; public class ClassPropertyTest { //Single Object @Test public void testClassProperty() { Book obj […]

Read more JUnit – Assert if a property exists in a class

In JUnit, you can use @FixMethodOrder(MethodSorters.NAME_ASCENDING) to run the test methods by method name, in lexicographic order. P.S Tested with JUnit 4.12 ExecutionOrderTest.java package com.mkyong; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; //Sorts by method name @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ExecutionOrderTest { @Test public void testB() { assertThat(1 + 1, is(2)); […]

Read more JUnit – Run test in a particular order

Forget about JUnit assertEquals(), to test a Map, uses the more expressive IsMapContaining class from hamcrest-library.jar pom.xml <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> </exclusion> </exclusions> </dependency> <!– This will get hamcrest-core automatically –> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>test</scope> </dependency> </dependencies> 1. IsMapContaining Examples All the below assertThat checks will be passed. […]

Read more JUnit – How to test a Map

First, exclude the JUnit bundled copy of hamcrest-core, and include the useful hamcrest-library, it contains many useful methods to test the List data type. pom.xml <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> </exclusion> </exclusions> </dependency> <!– This will get hamcrest-core automatically –> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>test</scope> </dependency> </dependencies> 1. Assert List […]

Read more JUnit – How to test a List

In Maven, you can declare the JUnit dependency like this: pom.xml <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> But, it comes with a bundled copy of hamcrest-core library. $ mvn dependency:tree … [INFO] \- junit:junit:jar:4.12:test [INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test … 1. Maven + JUnit + Hamcrest Note Not a good idea to use the default […]

Read more Maven and JUnit example

In Gradle, you can declare the JUnit dependency like this: build.gradle apply plugin: ‘java’ dependencies { testCompile ‘junit:junit:4.12’ } By default, JUnit comes with a bundled copy of hamcrest-core $ gradle dependencies –configuration testCompile testCompile – Compile classpath for source set ‘test’. \— junit:junit:4.12 \— org.hamcrest:hamcrest-core:1.3 1. Gradle + JUnit + Hamcrest Normally, we need […]

Read more Gradle and JUnit example

In this tutorial, we will show you how to run a junit test in Ant build. 1. Run a unit test build.xml <target name="junit" depends="compile"> <junit printsummary="yes" haltonfailure="no"> <!– Project classpath, must include junit.jar –> <classpath refid="test.path" /> <!– test class –> <classpath location="${test.classes.dir}" /> <test name="com.mkyong.test.TestMessage" haltonfailure="no" todir="${report.dir}"> <formatter type="plain" /> <formatter type="xml" /> […]

Read more Ant and jUnit Task example

Declares a junit task in Ant like this build.xml <!– Run jUnit –> <target name="junit" depends="resolve"> <junit printsummary="yes" haltonfailure="no"> <classpath refid="test.path" /> <classpath location="${build.dir}" /> <test name="com.mkyong.test.TestMessage" haltonfailure="no" todir="${report.dir}" outfile="result"> <formatter type="plain" /> <formatter type="xml" /> </test> </junit> </target> Run ant junit, but hits the following error message : BUILD FAILED build.xml:86: The <classpath> for […]

Read more Must include junit.jar if not in Ant’s own classpath

In JUnit, you can run multiple test cases with @RunWith and @Suite annotation. Refer to the following examples : SuiteAbcTest.java package com.mkyong; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ Exception1Test.class, //test case 1 TimeoutTest.class //test case 2 }) public class SuiteAbcTest { //normally, this is an empty class } P.S Tested with JUnit 4.12 […]

Read more JUnit – Suite Test, run multiple test cases

If a test is taking longer than a defined “timeout” to finish, a TestTimedOutException will be thrown and the test marked failed. See the following example : P.S Tested with JUnit 4.12 1. Timeout Example This timeout example only applies to a single test method. And the timeout value is in milliseconds. TimeoutTest.java package com.mkyong; […]

Read more JUnit – Timeout Test

In JUnit, to ignore a test, just add a @Ignore annotation before or after the @Test method. P.S Tested with JUnit 4.12 IgnoreTest.java package com.mkyong; import org.junit.Ignore; import org.junit.Test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public class IgnoreTest { @Test public void testMath1() { assertThat(1 + 1, is(2)); } @Ignore @Test public void testMath2() { […]

Read more JUnit – Ignore a Test

In JUnit, there are 3 ways to test the expected exceptions : @Test, optional ‘expected’ attribute Try-catch and always fail() @Rule ExpectedException P.S Tested with JUnit 4.12 1. @Test expected attribute Use this if you only want to test the exception type, refer below : Exception1Test.java package com.mkyong; import org.junit.Test; import java.util.ArrayList; public class Exception1Test […]

Read more JUnit – Expected Exceptions Test