StudentUnitTest.java 1.81 KB
package com.krunal.reactive;

import com.krunal.reactive.model.Student;
import com.krunal.reactive.repository.StudentRepository;
import com.krunal.reactive.service.StudentServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentUnitTest {
    private static final Student STUDENT = new Student(31L, "Praful", "12");
    private static final Mono<Student> MONO_STUDENT = Mono.just(STUDENT);
    private static final Flux<Student> FLUX_STUDENT = Flux.just(STUDENT);
    private static final Long ID = 31L;

    @Autowired
    StudentServiceImpl studentService;

    @MockBean
    StudentRepository studentRepository;


    @Test
    public void saveStudentTest() {
        when(studentRepository.save(STUDENT)).thenReturn(MONO_STUDENT);
        assertEquals(MONO_STUDENT,studentService.saveStudent(STUDENT));
        verify(studentRepository).save(STUDENT);
    }

    @Test
    public void getStudentTest() {
        when(studentRepository.findById(ID)).thenReturn(MONO_STUDENT);
        assertEquals(MONO_STUDENT,studentService.getStudent(ID));
        verify(studentRepository).findById(ID);
    }

    @Test
    public void studentsTest() {
        when(studentRepository.findAll()).thenReturn(FLUX_STUDENT);
        assertEquals(FLUX_STUDENT,studentService.getStudents());
        verify(studentRepository).findAll();
    }

}