Springboot API, Lombok, Junit Mockito Test

5 min readMay 25


A mash of syntaxes to remember and reference….

Photo by Carlos Alfonso on Unsplash


Junit is typically used with other libraries like Mockito

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.mockito.junit.jupiter.MockitoExtension;

class ATest{

private DietPlanner dietPlanner;

@Mock GitFileDownloader gitFileDownloader;

@Captor private ArgumentCaptor<List<GitFileInfo>> gitFileArgumentCaptor;

void setup(){
this.dietPlanner = new DietPlanner();

void test(){

void testWithException(){
Executable e = () -> A.xx();

assertThrows(ArithmeticException.class, e);

void copyNewString(
final String sourceFileResource,
final String targetFileResource,
final String expectedOutputFileResource,
final String sourceMsgId)
throws Exception {


private static Stream<Arguments> getArgumentsForCopyNewString() {
return Stream.of(

void testB(){
when(gitFileDownloader.downloadLocaleFile("123", "branch", "source-locale"))


void testWithArgumentCapture(){
// pretend gitFileUploader was initialised...
final List<GitFileInfo> gitFileInfosOutput = gitFileArgumentCaptor.getValue();


void givenASpy_whenStubbingTheBehaviour_thenCorrect() {
List<String> list = new ArrayList<String>();
List<String> spyList = spy(list);

assertEquals(0, spyList.size());


mocks: when(mock.method()).thenReturn()


Mock VS Spy

stub means defining a behavior when some function is called. when(mockList.get(100)).thenReturn(expected);

  • By default, calling the methods of mock object will do nothing (When there is no stub). If it is a method, it will not return anything…it returns null.
  • Spy object will call the real method when not stub.

If you want to be safe and avoid calling external services and just want to test the logic inside of the unit, then use mock. If you want to call external service and perform calling of real dependency, or simply say, you want to run the program as it is and just stub specific methods, then use spy.


A JUnit test class to test the Spring MVC controller request and responses can use the below-given configuration.

public class TestEmployeeRESTController {

private MockMvc mvc;

public void getAllEmployeesAPI() throws Exception {
// I don't really like using MockMVCResultMatcher.jsonpath....
// I prefer to use andReturn();

public void getEmployeeByIdAPI() throws Exception {
mvc.perform( MockMvcRequestBuilders
.get("/employees/{id}", 1)

public void createEmployeeAPI() throws Exception {
mvc.perform( MockMvcRequestBuilders
.content(asJsonString(new EmployeeVO(null, "firstName4", "lastName4", "email4@mail.com")))

public static String asJsonString(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException(e);

void shouldReturn200WhenRequestToOutsourceTranslationToXtmIsReceived() throws Exception {
final String requestBody =
+ " \"ticketNumber\": \"PENGUIN-1234\",\n"
+ " \"projectManagerId\": \"12345\",\n"
+ " \"hasAutoStart\": \"true\"\n"
+ "}";
MvcResult result =
mockMvc.perform(postRequest(requestBody, "/toXtm"))

assertEquals(result.getResponse().getContentAsString(), "true");

private MockHttpServletRequestBuilder postRequest(final String content, final String endpoint) {
return post(BASE_URL + endpoint)

import aero.airlab.challenge.conflictforecast.api.Conflict;
import aero.airlab.challenge.conflictforecast.api.ConflictForecastRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.kotlin.KotlinModule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import src.ConflictController;
import src.ConflictService;

import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ConflictControllerTest {
private ConflictController conflictController;

private ConflictService conflictService;

void setUp() {
conflictService = new ConflictService();
conflictController = new ConflictController(this.conflictService);

public void testCheckConflict() throws Exception {

MockHttpServletRequest request = new MockHttpServletRequest();
final ResponseEntity<List<Conflict>> response = conflictController.conflict(getRequestObjectFromJsonFile("demo-request-3.json"));
assertEquals(response.getStatusCode(), HttpStatus.OK);


private ConflictForecastRequest getRequestObjectFromJsonFile(String path) throws IOException {
ObjectMapper objectMapper = new ObjectMapper().registerModule(new KotlinModule());
File file = new File(getClass().getClassLoader().getResource(path).getFile());
return objectMapper.readValue(file, ConflictForecastRequest.class);

Gradle imports

    // Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'

testCompile 'junit:junit'
testCompile 'org.assertj:assertj-core'
testCompile 'org.junit.jupiter:junit-jupiter-params'

// Tests
testCompile 'org.mockito:mockito-inline'


@GetMapping(value = "/employees")
public EmployeeListVO getAllEmployees(){

@GetMapping(value = "/employees/{id}")
public ResponseEntity<EmployeeVO> getEmployeeById (@PathVariable("id") int id){

@PostMapping(value = "/employees")
public ResponseEntity<EmployeeVO> addEmployee (@RequestBody EmployeeVO employee){
return new ResponseEntity<EmployeeVO>(employee, HttpStatus.CREATED);


public class XtmJiraLink {
@Id private ObjectId id;

private String xtmProjectId;

private String jira;

private ObjectId projectId; // id in string-db.project

private DateTime createdTime;


@NoArgsConstructor: This annotation generates a no-argument constructor for a class. This is useful when you need to instantiate a bean without providing any arguments.

@AllArgsConstructor: This annotation generates a constructor with arguments for all of the fields in a class. This is useful when you need to instantiate a bean and provide values for all of its fields.

@RequiredArgsConstructor: This annotation generates a constructor with arguments for all of the final fields in a class. This is useful when you have a class with many final fields and want to enforce that they are initialized at construction time.

@Builder: This annotation generates a builder class that allows you to construct objects with a more readable and concise syntax. This is useful when you have many fields in a class and want to provide a flexible and intuitive way to construct instances.

@Value: This annotation generates an immutable class with a constructor for all of the fields. This is useful when you have a class with many fields that should not be modified after construction.

@Data: This annotation generates boilerplate code for getters, setters, equals, hashCode, and toString methods. This is useful when you have a class with many fields and want to avoid writing repetitive code.

Object Mapper

implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
// We can also expose objectMapper as a bean in App Config

public class AppConfig{
public ObjectMapper inputObjectMapper() {
return new ObjectMapper();

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

private final ObjectMapper objectMapper;

public static void main(String[] args) {

// Object to JSON
MyObject myObject = new MyObject();
String json = objectMapper.writeValueAsString(myObject);

// JSON to Object
String json = "{\"name\":\"John\",\"age\":30}";
MyObject myObject = objectMapper.readValue(json, MyObject.class);
private final RestTemplate restTemplate;

ResponseEntity<byte[]> res =
if (res.getStatusCode() != HttpStatus.OK) {
throw new Exception(
String.format("resp status: {}, resp body: {}",


public <T> HttpEntity<T> getRequestEntityForJson(final T values) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
return new HttpEntity<>(values, headers);

To quickly create a new spring boot project using Intellij, follow this





🌸 🌹 🪴 Dreamer. Creator. Software Engineer. Follow me to be entertained by my random sharing of my learnings, readings and personal reflections.