4
4
.
.
2
2
.
.
4
4
M
M
o
o
c
c
k
k
-
-
O
O
b
b
j
j
e
e
c
c
t
t
-
-
@
@
S
S
p
p
y
y
b
b
e
e
a
a
n
n
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to use
@Spy to Create Wrapper Object @Spybean PersonRepository personRepositorySpy
@InjectMocks to Inject Wrapper Object @Autowired MyController myController
when() to Specify Method Return Value when(personRepositorySpy.getPersonById(1)).thenReturn(...);
@SpyBean creates Wrapper Object around Existing Object (Instance of Dependency Class) & intercepts calls & return
value
every Method call is forwarded to a real Method of that Existing Object
but return value is blocked - instead return value defined by Mockito is returned
Application Schema [Result]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
Syntax
@SpringBootTest
class MyControllerTest {
@SpyBean PersonRepository personRepositoryMock;
@Autowired MyController myController;
MyController
http://localhost:8080/GetPerson
getPerson()
Person
Browser
Tomcat
MyControllerTest
@Spy
PersonRepository
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_mockito_spybean (add Spring Boot Starters from the table)
Create Package: entities (inside main package)
Create Class: Person.java (inside entities package)
Create Package: respositories (inside main package)
Create Class: PersonRepository.java (inside entities package)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside controllers package)
Create Package: controllers (inside test package src\test\java\com.ivoronline.springboot_junit)
Create Test Class: MyControllerTest.java (inside entities package)
Person.java
package com.ivoronline.springboot_mockito_spybean.entities;
public class Person {
//PROPERTIES
public Integer id;
public String name;
public Integer age;
//CONSTRUCTOR
public Person(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
PersonRepository.java
package com.ivoronline.springboot_mockito_spybean.respositories;
import com.ivoronline.springboot_mockito_spybean.entities.Person;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class PersonRepository {
//PROPERTY
Map<Integer, Person> persons = new HashMap();
//CONSTRUCTOR
public PersonRepository() {
persons.put(1, new Person(1, "John", 20));
persons.put(2, new Person(2, "Bill", 30));
persons.put(3, new Person(2, "Jack", 40));
}
//GET PERSON BY ID (this is method is called)
public Person getPersonById(Integer id) {
System.out.println("getPersonById()");
return persons.get(id);
}
}
MyController.java
package com.ivoronline.springboot_mockito_spybean.controllers;
import com.ivoronline.springboot_mockito_spybean.entities.Person;
import com.ivoronline.springboot_mockito_spybean.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired PersonRepository personRepository;
@ResponseBody
@RequestMapping("/GetPerson")
public String getPerson(@RequestParam Integer id) {
//GET PERSON
Person person = personRepository.getPersonById(id);
String name = person.name;
Integer age = person.age;
//RETURN SOMETHING
return name + " is " + age + " years old";
}
}
MyControllerTest.java
package com.ivoronline.springboot_mockito_spybean.controllers;
import com.ivoronline.springboot_mockito_spybean.entities.Person;
import com.ivoronline.springboot_mockito_spybean.repositories.PersonRepository;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.mockito.Mock;
import org.springframework.boot.test.mock.mockito.SpyBean;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class MyControllerTest {
//MOCK DEPENDENCY CLASS
@SpyBean PersonRepository personRepositoryMock;
//INJECT MOCKS (where @autowired is used)
@Autowired MyController myController;
@Test
void getPerson() {
//MOCK METHOD: getPersonById(1)
when(personRepositoryMock.getPersonById(1)).thenReturn(new Person(1, "Susan", 50));
//TEST CONTROLLER'S ENDPOINT
String result = myController.getPerson(1);
//TEST RESULT
assertEquals("Susan is 50 years old", result);
}
}