4
4
.
.
2
2
.
.
9
9
v
v
e
e
r
r
i
i
f
f
y
y
(
(
)
)
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to use verify() to check how many times Mocked Method was called with specific Parameter.
verify() only counts number of calls that happened before verify() was called.
Application Schema [Result]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
Syntax
verify(personRepositoryMock, times(2)).getPersonById(1);
Second Parameter
PARAMETER
EXAMPLE
DESCRIPTION
No Parameter
verify(personRepositoryMock ).getPersonById(1)
Exactly 1 time
times(2)
verify(personRepositoryMock, times(2) ).getPersonById(1)
Exactly 2 times
atLeast(2)
verify(personRepositoryMock, atLeast(2) ).getPersonById(1)
Twice or more
atLeastOnce()
verify(personRepositoryMock, atLeastOnce()).getPersonById(1)
Once or more
never()
verify(personRepositoryMock, never() ).getPersonById(1)
Never
http://localhost:8080/GetPerson
getPerson()
PersonRepository
Person
Browser
Tomcat
MyControllerTest
@Mock
PersonRepository
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_mockito_verify (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)
Create Test Class: MyControllerTest.java (inside entities package)
Person.java
package com.ivoronline.springboot_mockito_verify.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_verify.respositories;
import com.ivoronline.springboot_mockito_verify.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(3, "Jack", 40));
}
//GET PERSON BY ID
public Person getPersonById(Integer id) {
return persons.get(id);
}
}
MyController.java
package com.ivoronline.springboot_mockito_verify.controllers;
import com.ivoronline.springboot_mockito_verify.entities.Person;
import com.ivoronline.springboot_mockito_verify.respositories.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_verify.controllers;
import com.ivoronline.springboot_mockito_verify.entities.Person;
import com.ivoronline.springboot_mockito_verify.respositories.PersonRepository;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.springframework.boot.test.context.SpringBootTest;
import org.mockito.Mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@SpringBootTest
class MyControllerTest {
//MOCK DEPENDENCY CLASS
@Mock PersonRepository personRepositoryMock;
//INJECT MOCKS (where @autowired is used)
@InjectMocks MyController myController;
//ENDPOINT
@Test
void getPerson() {
//MOCK REPOSITORY METHOD
when(personRepositoryMock.getPersonById(1)).thenReturn(new Person(1, "Susan", 50));
//CALL REPOSITORY METHOD 2 TIMES
myController.getPerson(1);
myController.getPerson(1);
//VERIFY THAT METHOD getPersonById() WAS CALLED EXACTLY 2 TIMES WITH PARAMETER 1
verify(personRepositoryMock, times(2)).getPersonById(1);
}
}
R
R
e
e
s
s
u
u
l
l
t
t
http://localhost:8080/GetPerson?id=1
Run Test Class: MyControllerTest.java
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>