JUnit + Spring integration example

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 – Categories Test

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 – Assert if a property exists in a class

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 – Run test in a particular order

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 – How to test a Map

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 List

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

Hamcrest – How to assertThat check null value?

Try to check null value with the Hamcrest assertThat assertion, but no idea how? @Test public void testApp() { //ambiguous method call? assertThat(null, is(null)); } 1. Solution 1.1 To check null value, try is(nullValue), remember static import org.hamcrest.CoreMatchers.* //hello static import, nullValue here import static org.hamcrest.CoreMatchers.*; import org.junit.Test; //… @Test public void testApp() { //true, …

Read more

Maven and JUnit example

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

Gradle 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

TestNG + Spring Integration Example

In this tutorial, we will show you how to test Spring’s components with TestNG. Tools used : TestNG 6.8.7 Spring 3.2.2.RELEASE Maven 3 Eclipse IDE 1. Project Dependencies To integrate Spring with TestNG, you need spring-test.jar, add the following : pom.xml <properties> <spring.version>3.2.2.RELEASE</spring.version> <testng.version>6.8.7</testng.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> …

Read more

TestNG – Groups Test

In this tutorial, we will show you how to do the group testing in TestNG. 1. Groups on Methods Review a test group example. runSelenium() and runSelenium1() are belong to group selenium-test. testConnectOracle() and testConnectMsSQL() are belong to group database. runFinal() will be executed if groups selenium-test and database are passed. TestGroup.java package com.mkyong.testng.examples.group; import …

Read more

TestNG Hello World Example

A classic example, show you how to get started with TestNG unit test framework. Tools used : TestNG 6.8.7 Maven 3 Eclipse IDE 1. TestNG Dependency Add TestNG library in the pom.xml. pom.xml <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.7</version> <scope>test</scope> </dependency> 2. TestNG Example Review a simple class, has a method to return a fixed email “[email protected]”. …

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

JUnit 4 Vs TestNG – Comparison

JUnit 4 and TestNG are both very popular unit test framework in Java. Both frameworks look very similar in functionality. Which one is better? Which unit test framework should I use in Java project? Here I did a feature comparison between JUnit 4 and TestNG. 1. Annotation Support The annotation supports are implemented in both …

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

JUnit – Suite Test, run multiple test cases

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 – Timeout Test

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 – Ignore a 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 – Expected Exceptions 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 – Basic annotation examples

Here’re some basic JUnit annotations you should understand: @BeforeClass – Run once before any of the test methods in the class, public static void @AfterClass – Run once after all the tests in the class have been run, public static void @Before – Run before @Test, public void @After – Run after @Test, public void …

Read more

TestNG – Dependency Test

In TestNG, we use dependOnMethods and dependsOnGroups to implement dependency testing. If a dependent method is fail, all the subsequent test methods will be skipped, NOT failed. 1. dependOnMethods Example A simple example, “method2()” is dependent on “method1()”. 1.1 If method1() is passed, method2() will be executed. App.java package com.mkyong.testng.examples.dependency; import org.testng.annotations.Test; public class App …

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

TestNG – Run multiple test classes (suite test)

In this tutorial, we will show you how to run multiple TestNG test cases (classes) together, aka suite test. 1. Test Classes Review following three test classes. TestConfig.java package com.mkyong.testng.examples.suite; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; //show the use of @BeforeSuite and @BeforeTest public class TestConfig { @BeforeSuite public void testBeforeSuite() { System.out.println("testBeforeSuite()"); …

Read more

TestNG – Timeout Test

In this tutorial, we will show you how to perform timeout test in TestNG. The “timeout” means if a unit test is taking longer than a specified number of milliseconds to finish, TestNG will abort it and market it as failed. This “timeout” can also use for performance test, to ensure the method is returned …

Read more

TestNG – How to ignore a test method

In this tutorial, we will show you how to ignore a test method with @Test(enabled = false). TestIgnore.java package com.mkyong.testng.examples.ignore; import org.testng.Assert; import org.testng.annotations.Test; public class TestIgnore { @Test //default enable=true public void test1() { Assert.assertEquals(true, true); } @Test(enabled = true) public void test2() { Assert.assertEquals(true, true); } @Test(enabled = false) public void test3() { …

Read more

TestNG – Expected Exception Test

In this tutorial, we will show you how to use the TestNG expectedExceptions to test the expected exception throws in your code. 1. Runtime Exception This example shows you how to test a runtime exception. If the method divisionWithException () throws a runtime exception – ArithmeticException, it will be passed. TestRuntime.java package com.mkyong.testng.examples.exception; import org.testng.annotations.Test; …

Read more