Spring Boot: Unit Testing and Mocking With Mockito and JUnit
In this post we take an in-depth look at how to create effective unit tests in Spring Boot using JUnit and Mockito. Read on for the details!
Join the DZone community and get the full member experience.
Join For Freethis guide will help you create great unit tests with junit and mockito for your spring boot projects. we will use two different approaches to write unit tests.
you will learn:
- what is junit?
- what is mockito?
- how to create a simple spring boot project with unit testing.
- how to write unit tests with mockito using @mock and @injectmocks without launching up a spring context.
- how to write a unit test with mocking and launching up the complete spring context using @mockbean.
10-step reference courses
- spring framework for beginners in 10 steps
- spring boot for beginners in 10 steps
- spring mvc in 10 steps
- jpa and hibernate in 10 steps
- eclipse tutorial for beginners in 5 steps
- maven tutorial for beginners in 5 steps
- junit tutorial for beginners in 5 steps
- mockito tutorial for beginners in 5 steps
- complete in28minutes course guide
project code structure
the following screenshot shows the structure of the project we will create.
a few details:
businessservice.java
- the business service that we want to test.dataservice.java
- dataservice is a dependency of the businessservice. we would want to mock the dataservice in our unit tests.businessservicemocktest.java
- unit test using basic mocking with @mock and @injectmocks. this unit test does not launch up a spring context.businessservicemockspringcontexttest.java
- unit test launching the complete spring context to test businessservice.springboottutorialbasicsapplication.java
- the main spring boot application class which is used to launch up the application.pom.xml
- contains all the dependencies needed to build this project. we will use spring boot starter web and test other than developer tools.- maven 3.0+ is your build tool.
- your favorite ide. we use eclipse.
- jdk 1.8+
introduction to junit
junit is the most popular java unit testing framework.
we typically work in large projects - some of these projects have more than 2,000 source files or sometimes it might be as big as 10,000 files with one million lines of code.
before unit testing, we depend on deploying the entire app and checking if the screens look great. but that’s not very efficient, and it is manual. unit testing focuses on writing automated tests for individual classes and methods. junit is a framework which will help you call a method and check (or assert) whether the output is as expected. the important thing about automation testing is that these tests can be run with continuous integration - as soon as some code changes.
example source code to test:
package com.in28minutes.junit;
public class mymath {
int sum(int[] numbers) {
int sum = 0;
for (int i: numbers) {
sum += i;
}
return sum;
}
}
unit test for the sum method:
package com.in28minutes.junit;
import static org.junit.assert.assertequals;
import org.junit.after;
import org.junit.afterclass;
import org.junit.before;
import org.junit.beforeclass;
import org.junit.test;
public class mymathtest {
mymath mymath = new mymath();
// mymath.sum
// 1,2,3 => 6
@test
public void sum_with3numbers() {
system.out.println("test1");
assertequals(6, mymath.sum(new int[] {
1,
2,
3
}));
}
@test
public void sum_with1number() {
system.out.println("test2");
assertequals(3, mymath.sum(new int[] {
3
}));
}
}
other important junit annotations
- @before @after annotations
- run before and after every test method in the class
- @beforeclass @afterclass annotations
- static methods which are executed once before and after a test class
package com.in28minutes.junit;
import static org.junit.assert.assertequals;
import org.junit.after;
import org.junit.afterclass;
import org.junit.before;
import org.junit.beforeclass;
import org.junit.test;
public class mymathtest {
mymath mymath = new mymath();
@before
public void before() {
system.out.println("before");
}
@after
public void after() {
system.out.println("after");
}
@beforeclass
public static void beforeclass() {
system.out.println("before class");
}
@afterclass
public static void afterclass() {
system.out.println("after class");
}
// mymath.sum
// 1,2,3 => 6
@test
public void sum_with3numbers() {
system.out.println("test1");
assertequals(6, mymath.sum(new int[] {
1,
2,
3
}));
}
@test
public void sum_with1number() {
system.out.println("test2");
assertequals(3, mymath.sum(new int[] {
3
}));
}
}
introduction to mockito
mockito is the most popular mocking framework in java.
in the example below somebusinessimpl depends on dataservice. when we write a unit test for somebusinessimpl, we will want to use a mock dataservice — one which does not connect to a database.
package com.in28minutes.mockito.mockitodemo;
public class somebusinessimpl {
private dataservice dataservice;
public somebusinessimpl(dataservice dataservice) {
super();
this.dataservice = dataservice;
}
writing a test with mockito
package com.in28minutes.mockito.mockitodemo;
import static org.junit.assert.assertequals;
import static org.mockito.mockito.mock;
import static org.mockito.mockito.when;
import org.junit.test;
public class somebusinessmocktest {
@test
public void testfindthegreatestfromalldata() {
dataservice dataservicemock = mock(dataservice.class);
when(dataservicemock.retrievealldata()).thenreturn(new int[] {
24,
15,
3
});
somebusinessimpl businessimpl = new somebusinessimpl(dataservicemock);
int result = businessimpl.findthegreatestfromalldata();
assertequals(24, result);
}
@test
public void testfindthegreatestfromalldata_foronevalue() {
dataservice dataservicemock = mock(dataservice.class);
when(dataservicemock.retrievealldata()).thenreturn(new int[] {
15
});
somebusinessimpl businessimpl = new somebusinessimpl(dataservicemock);
int result = businessimpl.findthegreatestfromalldata();
assertequals(15, result);
}
}
notes
dataservice dataservicemock = mock(dataservice.class)
- we are using the mock method to create a mock.when(dataservicemock.retrievealldata()).thenreturn(new int[] { 24, 15, 3 })
- stubbing the mock to return specific data
using mockito annotations - @mock, @injectmocks, @runwith(mockitojunitrunner.class)
package com.in28minutes.mockito.mockitodemo;
import static org.junit.assert.assertequals;
import static org.mockito.mockito.when;
import org.junit.test;
import org.junit.runner.runwith;
import org.mockito.injectmocks;
import org.mockito.mock;
import org.mockito.junit.mockitojunitrunner;
@runwith(mockitojunitrunner.class)
public class somebusinessmockannotationstest {
@mock
dataservice dataservicemock;
@injectmocks
somebusinessimpl businessimpl;
@test
public void testfindthegreatestfromalldata() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {
24,
15,
3
});
assertequals(24, businessimpl.findthegreatestfromalldata());
}
@test
public void testfindthegreatestfromalldata_foronevalue() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {
15
});
assertequals(15, businessimpl.findthegreatestfromalldata());
}
@test
public void testfindthegreatestfromalldata_novalues() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {});
assertequals(integer.min_value, businessimpl.findthegreatestfromalldata());
}
}
notes
@mock dataservice dataservicemock;
- create a mock for dataservice.@injectmocks somebusinessimpl businessimpl;
- inject the mocks as dependencies into businessimpl.@runwith(mockitojunitrunner.class)
- the junit runner which causes all the initialization magic with @mock and @injectmocks to happen before the tests are run.
creating the project with spring initializr
creating a rest service with spring initializr is a cake walk.
spring initializr is great tool to bootstrap your spring boot projects.
as shown in the image above, following steps have to be done
- launch spring initializr and choose the following
- choose
com.in28minutes.springboot.tutorial.basics.example
as group - choose
spring-boot-tutorial-basics
as artifact - choose following dependencies
- web
- devtools
- choose
- click generate project.
- import the project into eclipse. file -> import -> existing maven project.
starter projects in pom.xml
below are a couple ofstarter projects in pom.xml.
< dependency >
<
groupid > org.springframework.boot < /groupid> <
artifactid > spring - boot - starter - web < /artifactid> <
/dependency> <
dependency >
<
groupid > org.springframework.boot < /groupid> <
artifactid > spring - boot - starter - test < /artifactid> <
scope > test < /scope> <
/dependency>
the important dependency for unit testing is spring-boot-starter-test
spring boot test starter is starter for testing spring boot applications with libraries including junit, hamcrest and mockito. let’s look at important dependencies in spring-boot-starter-test.
< dependency >
<
groupid > junit < /groupid> <
artifactid > junit < /artifactid> <
version > 4.12 < /version> <
scope > compile < /scope> <
/dependency> <
dependency >
<
groupid > org.assertj < /groupid> <
artifactid > assertj - core < /artifactid> <
version > 3.8 .0 < /version> <
scope > compile < /scope> <
/dependency> <
dependency >
<
groupid > org.mockito < /groupid> <
artifactid > mockito - core < /artifactid> <
version > 2.11 .0 < /version> <
scope > compile < /scope> <
/dependency> <
dependency >
<
groupid > org.hamcrest < /groupid> <
artifactid > hamcrest - core < /artifactid> <
version > 1.3 < /version> <
scope > compile < /scope> <
/dependency> <
dependency >
<
groupid > org.hamcrest < /groupid> <
artifactid > hamcrest - library < /artifactid> <
version > 1.3 < /version> <
scope > compile < /scope> <
/dependency> <
dependency >
<
groupid > org.skyscreamer < /groupid> <
artifactid > jsonassert < /artifactid> <
version > 1.5 .0 < /version> <
scope > compile < /scope> <
/dependency> <
dependency >
<
groupid > org.springframework < /groupid> <
artifactid > spring - test < /artifactid> <
version > 5.0 .1.release < /version> <
scope > compile < /scope> <
/dependency>
spring boot test starter brings in a wide range of dependencies for unit testing.
- basic test framework - junit
- mocking - mockito
- assertion - assertj, hamcrest
- spring unit test framework - spring test
add the code under test
lets create a simple dataservice. actually this should talk to some database to get all the data, but for now lets keep it simple and return hardcode value. this will be a dependency for the businessservice.
/src/main/java/com/in28minutes/springboot/tutorial/basics/example/unittesting/dataservice.java
@repository
public class dataservice {
public int[] retrievealldata() {
// some dummy data
// actually this should talk to some database to get all the data
return new int[] {
1,
2,
3,
4,
5
};
}
}
let’s create the businessservice using dataservice as a dependency.
/src/main/java/com/in28minutes/springboot/tutorial/basics/example/unittesting/businessservice.java
@service
public class businessservice {
private dataservice dataservice;
public businessservice(dataservice dataservice) {
super();
this.dataservice = dataservice;
}
public int findthegreatestfromalldata() {
int[] data = dataservice.retrievealldata();
int greatest = integer.min_value;
for (int value: data) {
if (value > greatest) {
greatest = value;
}
}
return greatest;
}
}
important things to note:
public businessservice(dataservice dataservice) {
- we are providing a constructor for injecting the data service.public int findthegreatestfromalldata()
- this is the method we would want to write unit tests for. we would want to test with a wide range of combinations
unit testing with mockito using mockitorunner
code below shows a unit test with mockito using mockitojunitrunner.
/src/test/java/com/in28minutes/springboot/tutorial/basics/example/unittest/
businessservicesmocktest.java
notes
@runwith(mockitojunitrunner.class) public class businessservicesmocktest
- the junit runner which causes all the initialization magic with @mock and @injectmocks to happen before the tests are run.@mock dataservice dataservicemock
- create a mock for dataservice.@injectmocks businessservice businessimpl
- inject the mocks as dependencies into businessservice.- there are three test methods testing three different scenarios: multiple values, one value and no value passed in.
unit test launching the complete spring context using @mockbean
example code below shows how we can write the same unit test launching up the complete spring context.
/src/test/java/com/in28minutes/springboot/tutorial/basics/example/unittest/
businessservicesmockspringcontexttest.java
notes
@runwith(springrunner.class)
- spring runner is used to launch up a spring context in unit tests.@springboottest
- this annotation indicates that the context under test is a@springbootapplication
. the complete springboottutorialbasicsapplication is launched up during the unit test.@mockbean dataservice dataservicemock
- @mockbean annotation creates a mock for dataservice. this mock is used in the spring context instead of the real dataservice.@autowired businessservice businessimpl
- pick the business service from the spring context and autowire it in.
choosing between the approaches
launching the entire spring context makes the unit test slower. unit tests will also start failing if there are errors in other beans in the contexts. so, the mockitojunitrunner approach is preferred.
congratulations! you are reading an article from a series of 50+ articles on spring boot and microservices. we also have 20+ projects on our github repository. for the complete series of 50+ articles and code examples, click here .
next steps
- learn basics of spring boot - spring boot vs spring vs spring mvc , auto configuration , spring boot starter projects , spring boot starter parent , spring boot initializr
- learn restful and soap web services with spring boot
- learn microservices with spring boot and spring cloud
- watch spring framework interview guide - 200+ questions & answers
complete code example
/pom.xml
<< ? xml version = "1.0"
encoding = "utf-8" ? >
<
project xmlns = "http://maven.apache.org/pom/4.0.0"
xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance"
xsi: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<
modelversion > 4.0 .0 < /modelversion>
<
groupid > com.in28minutes.springboot.tutorial.basics.example < /groupid> <
artifactid > spring - boot - tutorial - basics < /artifactid> <
version > 0.0 .1 - snapshot < /version> <
packaging > jar < /packaging>
<
name > spring - boot - tutorial - basics < /name> <
description > spring boot tutorial - basic concept examples < /description>
<
parent >
<
groupid > org.springframework.boot < /groupid> <
artifactid > spring - boot - starter - parent < /artifactid> <
version > 2.0 .0.m6 < /version> <
relativepath / > <!-- lookup parent from repository -->
<
/parent>
<
properties >
<
project.build.sourceencoding > utf - 8 < /project.build.sourceencoding> <
project.reporting.outputencoding > utf - 8 < /project.reporting.outputencoding> <
java.version > 1.8 < /java.version> <
/properties>
<
dependencies >
<
dependency >
<
groupid > org.springframework.boot < /groupid> <
artifactid > spring - boot - starter - web < /artifactid> <
/dependency>
<
dependency >
<
groupid > org.springframework.boot < /groupid> <
artifactid > spring - boot - devtools < /artifactid> <
scope > runtime < /scope> <
/dependency> <
dependency >
<
groupid > org.springframework.boot < /groupid> <
artifactid > spring - boot - starter - test < /artifactid> <
scope > test < /scope> <
/dependency> <
/dependencies>
<
build >
<
plugins >
<
plugin >
<
groupid > org.springframework.boot < /groupid> <
artifactid > spring - boot - maven - plugin < /artifactid> <
/plugin> <
/plugins> <
/build>
<
repositories >
<
repository >
<
id > spring - snapshots < /id> <
name > spring snapshots < /name> <
url > https: //repo.spring.io/snapshot</url>
<
snapshots >
<
enabled > true < /enabled> <
/snapshots> <
/repository> <
repository >
<
id > spring - milestones < /id> <
name > spring milestones < /name> <
url > https: //repo.spring.io/milestone</url>
<
snapshots >
<
enabled > false < /enabled> <
/snapshots> <
/repository> <
/repositories>
<
pluginrepositories >
<
pluginrepository >
<
id > spring - snapshots < /id> <
name > spring snapshots < /name> <
url > https: //repo.spring.io/snapshot</url>
<
snapshots >
<
enabled > true < /enabled> <
/snapshots> <
/pluginrepository> <
pluginrepository >
<
id > spring - milestones < /id> <
name > spring milestones < /name> <
url > https: //repo.spring.io/milestone</url>
<
snapshots >
<
enabled > false < /enabled> <
/snapshots> <
/pluginrepository> <
/pluginrepositories>
<
/project>
/src/main/java/com/in28minutes/springboot/tutorial/basics/example/
springboottutorialbasicsapplication.java
package com.in28minutes.springboot.tutorial.basics.example;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.context.applicationcontext;
@springbootapplication
public class springboottutorialbasicsapplication {
public static void main(string[] args) {
applicationcontext applicationcontext = springapplication.run(springboottutorialbasicsapplication.class, args);
for (string name: applicationcontext.getbeandefinitionnames()) {
system.out.println(name);
}
}
}
/src/main/java/com/in28minutes/springboot/tutorial/basics/example/unittesting/
businessservice.java
package com.in28minutes.springboot.tutorial.basics.example.unittesting;
import org.springframework.stereotype.service;
@service
public class businessservice {
private dataservice dataservice;
public businessservice(dataservice dataservice) {
super();
this.dataservice = dataservice;
}
public int findthegreatestfromalldata() {
int[] data = dataservice.retrievealldata();
int greatest = integer.min_value;
for (int value: data) {
if (value > greatest) {
greatest = value;
}
}
return greatest;
}
}
/src/main/java/com/in28minutes/springboot/tutorial/basics/example/unittesting/
dataservice.java
package com.in28minutes.springboot.tutorial.basics.example.unittesting;
import org.springframework.stereotype.repository;
@repository
public class dataservice {
public int[] retrievealldata() {
// some dummy data
// actually this should talk to some database to get all the data
return new int[] {
1,
2,
3,
4,
5
};
}
}
/src/test/java/com/in28minutes/springboot/tutorial/basics/example/
springboottutorialbasicsapplicationtests.java
package com.in28minutes.springboot.tutorial.basics.example;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
@runwith(springrunner.class)
@springboottest
public class springboottutorialbasicsapplicationtests {
@test
public void contextloads() {}
}
/src/test/java/com/in28minutes/springboot/tutorial/basics/example/unittest/
businessservicesmockspringcontexttest.java
package com.in28minutes.springboot.tutorial.basics.example.unittest;
import static org.junit.assert.assertequals;
import static org.mockito.mockito.when;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.boot.test.mock.mockito.mockbean;
import org.springframework.test.context.junit4.springrunner;
import com.in28minutes.springboot.tutorial.basics.example.unittesting.businessservice;
import com.in28minutes.springboot.tutorial.basics.example.unittesting.dataservice;
@runwith(springrunner.class)
@springboottest
public class businessservicesmockspringcontexttest {
@mockbean
dataservice dataservicemock;
@autowired
businessservice businessimpl;
@test
public void testfindthegreatestfromalldata() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {
24,
15,
3
});
assertequals(24, businessimpl.findthegreatestfromalldata());
}
@test
public void testfindthegreatestfromalldata_foronevalue() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {
15
});
assertequals(15, businessimpl.findthegreatestfromalldata());
}
@test
public void testfindthegreatestfromalldata_novalues() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {});
assertequals(integer.min_value, businessimpl.findthegreatestfromalldata());
}
}
/src/test/java/com/in28minutes/springboot/tutorial/basics/example/unittest/
businessservicesmocktest.java
package com.in28minutes.springboot.tutorial.basics.example.unittest;
import static org.junit.assert.assertequals;
import static org.mockito.mockito.when;
import org.junit.test;
import org.junit.runner.runwith;
import org.mockito.injectmocks;
import org.mockito.mock;
import org.mockito.junit.mockitojunitrunner;
import com.in28minutes.springboot.tutorial.basics.example.unittesting.businessservice;
import com.in28minutes.springboot.tutorial.basics.example.unittesting.dataservice;
@runwith(mockitojunitrunner.class)
public class businessservicesmocktest {
@mock
dataservice dataservicemock;
@injectmocks
businessservice businessimpl;
@test
public void testfindthegreatestfromalldata() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {
24,
15,
3
});
assertequals(24, businessimpl.findthegreatestfromalldata());
}
@test
public void testfindthegreatestfromalldata_foronevalue() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {
15
});
assertequals(15, businessimpl.findthegreatestfromalldata());
}
@test
public void testfindthegreatestfromalldata_novalues() {
when(dataservicemock.retrievealldata()).thenreturn(new int[] {});
assertequals(integer.min_value, businessimpl.findthegreatestfromalldata());
}
}
Published at DZone with permission of Ranga Rao Karanam. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments