Home > Software engineering >  I want to write a mockito test case for a spring boot service method
I want to write a mockito test case for a spring boot service method

Time:12-03

This is the service method.

    @Override
    public void removeStudentByID(Long studentId) throws StudentExistsException {
        boolean exists = studentRepo.existsById(studentId);
        if (!exists) {
            throw new StudentExistsException("Student doesn't exist");
        }
        studentRepo.deleteById(studentId);
    }

Below is the test case I've written for it.

@Test
    public void removeStudentByIdTest() throws StudentExistsException {
        Student student = new Student(1, "Drake", "[email protected]");
        when(studentRepo.save(student)).thenReturn(student);
        studentService.removeStudentByID(student.getId());
        assertEquals(false, studentRepo.existsById(student.getId()));
    }

It doesn't run... But the below code works.

    public void removeStudent(String email) {
        Student student = studentRepo.findByEmail(email);
        studentRepo.delete(student);
    }
@Test
    public void removeStudentTest() throws StudentExistsException {
        Student student = new Student(3, "Ben", "[email protected]");
        studentRepo.save(student);
        studentService.removeStudent(student.getEmail());
        assertEquals(false, studentRepo.existsById(student.getId()));
    }

So, being a noob, I'm kinda confused as to how to make it work in the first one. Or is something wrong with both of them.

CodePudding user response:

To test this code:

@Service
public class StudentService { 
  @Override
  public void removeStudentByID(Long studentId) throws StudentExistsException 
    boolean exists = studentRepo.existsById(studentId);
    if (!exists) {
        throw new StudentExistsException("Student doesn't exist");
    }
    studentRepo.deleteById(studentId);
  }
}

We can mock/"unit test" like this:

package com.example.demo;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class StudenServiceTests { // we only test StudentService, since we (sort of) trust studentRepository (tested it elsewehere/how)

  @Mock // A Mock! (alternatively @MockBean)
  private StudentRepository studentRepoMock;
  @InjectMocks // A real service (with mocks injected;)!!! (alternatively @Autowired/...)
  private StudentService testee; // this guy we want to test (to the bone!)

  @Test // ..the "good case"
  void removeStudentByIdExists() throws StudentExistsException {
    // Given:
    // we don't even need a student, only his id
    // instruct mock for the "first interaction" (see testee code):
    when(studentRepoMock.existsById(1L)).thenReturn(true);
    // instruct mock for "second interaction" (see testee code):
    doNothing().when(studentRepoMock).deleteById(1L); // we could also use ...delete(any(Long.class))  or ..anyLong();

    // When: (do it!)
    testee.removeStudentByID(1L);

    // Then: Verify (interactions, with as exact as possible parameters ..Mockito.any***)
    verify(studentRepoMock).existsById(1L); //.times(n) ...when you had a list ;)
    verify(studentRepoMock).deleteById(1L);
  }

  @Test // .. the "bad (but correct) case"/exception
  void removeStudentByIdNotExists() throws StudentExistsException {
    // Given:
    when(studentRepoMock.existsById(1L)).thenReturn(false);

    // When:
    StudentExistsException thrown = assertThrows(
            StudentExistsException.class,
            () -> testee.removeStudentByID(1L),
            "Expected testee.removeStudentByID to throw, but it didn't");
    // Then:
    assertNotNull(thrown);
    assertTrue(thrown.getMessage().contains("Student doesn't exist"));
  }
}

... run with mvn test.

With these two test cases, we achieve 100% test (& branch) coverage for the given method/service.


But don't mix it up with "integration test" (where we us a real repo/data base), then we can use:

An sample on "default" (assuming empty) database, with preparation (and @Rollback):

package com.example.demo;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;

@SpringBootTest
class StudentServiceIT { // .. here we try to test "all of our application" (as good as possible ..to the bone)

  @Autowired //! real "bean", no mock... this is only auxilary for this test, we could use some other method to prepare & verify
  private StudentRepository studentRepository;
  @Autowired //!! "real service"
  private StudentService studentService;

  @Test
  @Rollback // actually our test is "self-cleaning"...when no exceptions ;)
  void removeStudentByIdExists() throws StudentExistsException {
    // Given:
    Student student = new Student(1L, "Drake", "[email protected]");
    studentRepository.save(student);

    // When:
    studentService.removeStudentByID(1L);

    // Then:
    assertFalse(studentRepository.findById(1L).isPresent());
  }

  @Test
  @Rollback // in any case (if assumptions were wrong;)
  void removeStudentByIdNotExists() throws StudentExistsException {
    // Given:
    // nothing, we assume "empty db"

    // When:
    StudentExistsException thrown = assertThrows(
            StudentExistsException.class,
            () -> studentService.removeStudentByID(1L),
            "Expected testee.removeStudentByID to throw, but it didn't");
    // Then:
    assertNotNull(thrown);
    assertTrue(thrown.getMessage().contains("Student doesn't exist"));
  }
}

(Run with: mvn failsafe:integration-test)

Thanks to/Refs:


Complete Sample@github

CodePudding user response:

This is how I did it, lemme know if it's fine. :)

@Test
    public void removeStudentByIdTest() throws StudentExistsException {
        Student student = new Student(1, "Drake", "[email protected]");
        when(studentRepo.existsById(student.getId())).thenReturn(true);
        studentService.removeStudentByID(student.getId());
        verify(studentRepo).deleteById((long) 1);
    }
  • Related