본문 바로가기
SpringBoot

Spring boot Mockito Example - @Mock & @InjectMocks

by ByteBridge 2022. 5. 22.
반응형
@SpringBootTest
class StudentRepositoryTest {
    @Autowired
    Student student;
    @Autowired
    StudentGrades studentGrades;

	
    @Mock 
    private StudentRepository studentRepository;
	
    
    @InjectMocks // inject mock dependencies
    private StudentService studentService;

    @BeforeEach
    void beforeEach() {
        student.setFirstName("Eric");
        student.setLastName("King");
        student.setEmailAddress("eric.roby@asdf.com");
        student.setStudentGrades(studentGrades);
    }

    @DisplayName("When & Verify")
    @Test
    void assertEqualsTestAddGrades() {
        when(studentRepository.addGradeResultsForSingleClass(
            studentGrades.getMathGradeResults()))
        .thenReturn(100.00);

        assertEquals(100, studentService.addGradeResultsForSingleClass(
            student.getStudentGrades().getMathGradeResults()));

        verify(studentRepository).addGradeResultsForSingleClass(studentGrades.getMathGradeResults());

        verify(studentRepository, times(1)).addGradeResultsForSingleClass(studentGrades.getMathGradeResults());
    }
}
반응형

'SpringBoot' 카테고리의 다른 글

Spring boot Throwing Exception test  (0) 2022.05.22
Spring boot @MockBean  (0) 2022.05.22
Spring boot Test with Mockito  (0) 2022.05.22
Unit Test - ParameterizedTest  (0) 2022.05.21
Java Conditional Unit Test  (0) 2022.05.21