你应该能够测试你的具体反应。
假设,方法
courses
正在上课
Controller
。这是您正在测试的类,您的目标应该是在这里测试代码,这是您在控制器中编写的代码。
以下是一个示例:
package test;
import static org.junit.Assert.*;
import org.apache.catalina.connector.Response;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
public class ControllerTest {
@Mock
IStudentDAO iStudentDAO;
@InjectMocks
Controller controller;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testCoursesWhenStudentIDNotFound() {
Mockito.when(iStudentDAO.getStudentIdByToken("1234")).thenReturn(null);
Response response = controller.courses("1234");
assertEquals(403, response.getStatus())
}
}
类似地,在下一个测试用例中,您可以模拟
IStudentDAO
,以返回
studentId
然后是这方面的课程
学生ID
并验证您是否在响应中得到了他们。