StudentUnitTest.java
1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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();
}
}