IStudentServiceTest.java
1.63 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
package com.mock.example.mockdemo.service;
import com.mock.example.mockdemo.dao.StudentDao;
import com.mock.example.mockdemo.entity.Student;
import com.mock.example.mockdemo.service.impl.StudentServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("")
class IStudentServiceTest {
@InjectMocks
StudentServiceImpl studentService;
@Mock
StudentDao studentDao;
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getById() {
Student student = new Student();
student.setId("002");
student.setName("张三");
//when里面带的是条件,thenReturn里面表示的是返回结果
Mockito.when(studentDao.getById("002"))
.thenReturn(student);
//assertThat后面跟着断言的判断语句
Assertions.assertEquals(studentService.getById(student.getId()).getName(), "张三");
}
@Test
public void save() {
Student student = new Student();
student.setId("002");
student.setName("李四");
//when里面带的是条件,thenReturn里面表示的是返回结果
Mockito.when(studentDao.save(student))
.thenReturn("数据成功添加");
//assertThat后面跟着断言的判断语句
Assertions.assertEquals(studentService.save(student), "数据成功添加");
}
}