我刚接触用Java编写测试并尝试学习Mockito。
这是我要测试的课程的一部分。
public class ExamRepository implements IExamRepository {
private static final Logger LOGGER = LogManager.getLogger(ExamRepository.class);
private IStudentRepository studentRepository = new StudentRepository();
private EntityManager entityManager;
public ExamRepository() {
entityManager = EntityController.getEntityManager();
}
public ExamRepository(EntityManager entityManager){
this.entityManager = entityManager;
}
// Get all exam skeletons from the DB
@Override
public List<ExamSkeleton> getAllSkeletons() {
try {
TypedQuery<ExamSkeleton> query = entityManager.createQuery("SELECT NEW ExamSkeleton (s.id, s.filename, s.course, s.visible) FROM ExamSkeleton as s", ExamSkeleton.class);
return query.getResultList();
} catch (IllegalArgumentException exception) {
LOGGER.error(exception);
}
return Collections.emptyList();
}
}
这是我写的实际测试,我有一种感觉错误: 传递给verify()的参数是ExamRepository类型,不是mock!
因为这条线而发生:
examRepository = new ExamRepository(entityManager);
但是我不确定如何重写它。我也不确定我应该测试什么,除了它运行一次。
public class ExamRepositoryTest {
@InjectMocks
private ExamRepository examRepository;
@Mock
private EntityManager entityManager;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Test
public void canGetAllSkeletons(){
examRepository = new ExamRepository(entityManager);
List<ExamSkeleton> examSkeletons = new ArrayList<>();
examSkeletons.add(new ExamSkeleton());
examSkeletons.add(new ExamSkeleton());
TypedQuery query = mock(TypedQuery.class);
when(entityManager.createQuery(anyString(), Matchers.anyObject())).thenReturn(query);
when(query.getResultList()).thenReturn(examSkeletons);
verify(examRepository, times(1)).getAllSkeletons();
}
}
希望你们能把我放在正确的轨道上!
答案 0 :(得分:1)
像这样使用:
@Test
public void canGetAllSkeletons(){
examRepository = new ExamRepository(entityManager);
List<ExamSkeleton> expected = new ArrayList<>();
expected.add(new ExamSkeleton());
expected.add(new ExamSkeleton());
TypedQuery query = mock(TypedQuery.class);
when(entityManager.createQuery(anyString(), anyObject())).thenReturn(query);
when(query.getResultList()).thenReturn(expected);
List<ExamSkeleton> result = examRepository.getAllSkeletons();
assertEquals(expected, result);
}
您的想法几乎是正确的,但您不需要验证调用,而只需在测试中对examRepository.getAllSkeletons()
进行实际调用,并根据TypedQuery的预期返回值检查结果。
并且因为如果以前的模拟步骤有效,您只能获得正确的结果列表,您不需要验证EntityManager模拟上的任何调用。