junit 5 Latest mock mockito spring boot unit test

Spring Boot + JUnit 5 + Mockito – Mkyong.com

Spring Boot JUnit 5

On this article we show how the Spring Boot 2 integration check is carried out with JUnit 5 and in addition with Mockit.

  • Spring Boot 2.1.2. 19659004] Mockito 2
  • Maven 3

Briefly, exclude trains4 from a spring begin check and embrace the JUnit 5 Jupiter engine manually, made.

Let's take a look at the following Spring boot MVC network software and the way to perform a unit check with JUnit 5 and cleavage with a Mockito body

1. Maven

pom.xml

4.0.zero

com.mkyong.spring
testing-trains5-mockito
1.0

1.eight
5.three.2
2.24.zero [19659018] org.springframework.boot
spring-boot boot for father or mother
2.1.2.RELEASE

org.springframework.boot
spring-boot-boot-net

org .springframework. boot
spring boot boot check
check

trains
trains

org.junit.jupiter
JUnit-Jupiter engine
$ junit-jupiter.version
check

org.springframework.boot
bow-play-DevTools
true

org.springframework.boot
spring-play-maven extension

org.apache.maven .plugins
maven-surefire plugin
2.22.zero

Show venture dependencies

$ mvn dependence: tree

[INFO] com.mkyong.spring: testing-trains5-mockito: jar: 1.0
[INFO] + – org.springframework.boot: spring-boot-starter-net: jar: 2.1.2.RELEASE: compile
[INFO] | + – org.springframework.boot: spring-boot-starter: jar: 2.1.2.RELEASE: turn
[INFO] | | + – org.springframework.boot: spring-boot-starter-logging: jar: 2.1.2.RELEASE: compile
[INFO] | | | + – ch.qos.logback: logback-basic: jar: 1.2.3: translate
[INFO] | | | | – ch.qos.logback: logback-core: jar: 1.2.3: compile
[INFO] | | | + – org.apache.logging.log4j: log4j-to-slf4j: jar: 2.11.1: compile
[INFO] | | | | – org.apache.logging.log4j: log4j-api: jar: 2.11.1: compile
[INFO] | | | – org.slf4j: jul-to-slf4j: jar: 1.7.25: translate
[INFO] | | + – javax.annotation: javax.annotation-api: jar: 1.3.2: compile
[INFO] | | – org.yaml: snakeyaml: jar: 1.23: runtime
[INFO] | + – org.springframework.boot: spring-boot-starter-json: jar: 2.1.2.RELEASE: compile
[INFO] | | + – com.fasterxml.jackson.core: jackson-datasind: jar: 2.9.eight: compile
[INFO] | | | + – com.fasterxml.jackson.core: jackson: jar: 2.9.0: compile
[INFO] | | | com.fasterxml.jackson.core: jackson-core: jar: 2.9.eight: compile
[INFO] | | + – com.fasterxml.jackson.datatype: jackson-datatype-jdk8: jar: 2.9.eight: compile
[INFO] | | + – com.fasterxml.jackson.datatype: jackson-datatype-jsr310: jar: 2.9.eight: compile
[INFO] | | com.fasterxml.jackson.module: jackson-module-parameter-names: jar: 2.9.eight: assemble
[INFO] | + – org.springframework.boot: spring-boot-starter-tomcat: jar: 2.1.2.RELEASE: compile
[INFO] | | + – org.apache.tomcat.embed: tomcat-Embed-core: jar: 9.zero.14: compile
[INFO] | | + – org.apache.tomcat.embed: tomcat-embed: jar: 9.zero.14: compile
[INFO] | | – org.apache.tomcat.embed: tomcat-Embed-websocket: jar: 9.zero.14: translate
[INFO] | + – org.hibernate.validator: Hibernate-Validator: jar: 6.zero.14.Conclusion: compile
[INFO] | | + – javax.validation: validation-api: jar: 2.0.1. Conclusion: put together
[INFO] | | + – org.jboss.logging: jboss-logging: jar: three.3.2 Conclusion: assemble
[INFO] | | – com.fasterxml: Classmate: jar: 1.4.zero: Translate
[INFO] | + – org.springframework: spring-net: jar: 5.1.4.RELEASE: put collectively
[INFO] | | – org.springframework: spring-beans: jar: 5.1.4.RELEASE: put collectively
[INFO] | – org.springframework: spring-webmvc: jar: 5.1.four.RELEASE: assemble
[INFO] | + – org.springframework: spring-aop: jar: 5.1.4.RELEASE: compile
[INFO] | + – org.springframework: spring-context: jar: 5.1.4.RELEASE: compile
[INFO] | – org.springframework: spring-expression: jar: 5.1.4.RELEASE: compile
[INFO] + – org.springframework.boot: suspension start check: jar: 2.1.2.RELEASE: check
[INFO] | + – org.springframework.boot: spring-boot-check: jar: 2.1.2.RELEASE: check
[INFO] | + – org.springframework.boot: spring-boot-check-autoconfigure: jar: 2.1.2.RELEASE: check
[INFO] | + – com.jayway.jsonpath: json-path: jar: 2.4.0: check
[INFO] | | + – internet.minidev: json-sensible: jar: 2.3: check
[INFO] | | | – internet.minidev: accessors-sensible: jar: 1.2: check
[INFO] | | | – org.ow2.asm: asm: jar: 5.0.4: check
[INFO] | | – org.slf4j: slf4j-api: jar: 1.7.25: compile
[INFO] | + – org.assertj: assertj-core: jar: 3.11.1: check
[INFO] | + – org.mockito: mockito-core: jar: 2.24.zero: check
[INFO] | | + – internet.bytebuddy: byte-buddy: jar: 1.9.7: check
[INFO] | | + – internet.bytebuddy: byte-guy-agent: jar: 1.9.7: check
[INFO] | | – org.objenesis: objenesis: jar: 2.6: check
[INFO] | + – org.hamcrest: hamcrest-core: jar: 1.three: check
[INFO] | + – org.hamcrest: hamcrest-library: jar: 1.three: check
[INFO] | + – org.skyscreamer: jsonassert: jar: 1.5.0: check
[INFO] | | com.vaadin.external.google:android-json:jar:0.zero.20131108.vaadin1:check
[INFO] | + – org.springframework: spring-core: jar: 5.1.4.RELEASE: compile
[INFO] | | – org.springframework: spring-jcl: jar: 5.1.4.RELEASE: put collectively
[INFO] | + – org.springframework: spring check: jar: 5.1.4.RELEASE: check
[INFO] | – org.xmlunit: xmlunit-core: jar: 2.6.2: check
[INFO] | – javax.xml.bind: jaxb-api: jar: 2.3.1: check
[INFO] | – javax.activation: javax.activation-api: jar: 1.2.0: check
[INFO] + – org.junit.jupiter: trains-Jupiter engine: jar: 5.3.2: check
[INFO] | + – org.apiguardian: apiguardian-api: jar: 1.0.zero: check
[INFO] | + – org.junit.platform: trains-platform engine: jar: 1.three.2: check
[INFO] | | + – org.junit.platform: junit-platform-Commons: jar: 1.three.2: check
[INFO] | | – org.opentest4j: opentest4j: jar: 1.1.1: check
[INFO] | – org.junit.jupiter: junit-Jupiter-api: jar: 5.three.2: check
[INFO] org.springframework.boot: spring-boot-devtools: jar: 2.1.2.RELEASE: compile (elective)
[INFO] + – org.springframework.boot: spring-boot: jar: 2.1.2.RELEASE: compile
[INFO] org.springframework.boot: spring-boot-autoconfigure: jar: 2.1.2.RELEASE: assemble

2. Check the spring begin + JUnit 5 + Mockito

2.1 Check the next spring elements

HelloServiceImpl.java

package deal com.mkyong.core.providers;

import com.mkyong.core.repository.HelloRepository;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
Public class HelloServiceImpl implements HelloService

@Autowired
HelloRepository helloRepository;

@Bypass
public String get ()
restore helloRepository.get ();

HelloRepositoryImpl.java

package deal com.mkyong.core.repository;

import org.springframework.stereotype.Repository;

@Repository
public class HelloRepositoryImpl implement HelloRepository
@Bypass
public String get ()
again to "Hello JUnit 5";

2.2 JUnit 5

HelloServiceTest.java

package deal com.mkyong.core.providers;

deliver org.junit.jupiter.api.DisplayName;
deliver org.junit.jupiter.api.Check;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.boot.check.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
public class HelloServiceTest

@Autowired
HelloService helloService;

@DisplayName ("Test Spring @Autowired Integration")
@Check
void testGet ()
assertEquals ("Hello JUnit 5", helloService.get ());

2.three Mockito

HelloServiceMockTest.java

package deal com.mkyong.core.providers;

import com.mkyong.core.repository.HelloRepository;
convey org.junit.jupiter.api.All;
convey org.junit.jupiter.api.DisplayName;
deliver org.junit.jupiter.api.Check;
import org.mockito.InjectMocks;
deliver org.mockito.Mock;
import org.springframework.boot.check.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

@SpringBootTest
public class HelloServiceMockTest

@Mock
personal HelloRepository helloRepository;

@InjectMocks // automated injection helloRepository
personal HelloService helloService = new HelloServiceImpl ();

@BeforeEach
void setMockOutput ()
when (helloRepository.get ()) after which restore ("Hello Mockito From Responsitory");

@DisplayName ("Test Mock helloService + helloRepository")
@Check
void testGet ()
assertEquals ("Hello Mockito From Responsitory", helloService.get ());

3. MVC Controller Spring

three.1 MVC Controller

MainController.java

package deal com.mkyong.core.controller;

import org.springframework.stereotype.Controller;
import org.springframework.net.bind.annotation.GetMapping;
import org.springframework.net.bind.annotation.ResponseBody;

@Controller
generic MainController

@ResponseBody
@GetMapping ("/")
public String whats up ()
restore "Hello Controller";

three.2 JUnit 5 and MVC Check

MainControllerTest.java

package deal com.mkyong.core.controller;

convey org.junit.jupiter.api.Check;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.boot.check.context.SpringBootTest;
import org.springframework.boot.check.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.check.net.shopper.TestRestTemplate;
import org.springframework.boot.net.server.LocalServerPort;
import org.springframework.http.ResponseEntity;

import java.internet.URL;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest (webEnvironment = WebEnvironment.RANDOM_PORT)
public class MainControllerTest

// bind the above RANDOM_PORT
@LocalServerPort
personal int-port;

@Autowired
personal TestRestTemplate restTemplate;

@Check
public void getHello () throws an exception

ResponseEntity answer = restTemplate.getForEntity (
new URL ("http: // localhost:" + port + "/"). toString (), language tag);
assertEquals ("Hello Controller", reply.getBody ());

Carried out

Obtain Source

References