我刚开始学习在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,不是模拟对象!
发生这个错误的原因可能是以下代码行:
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();
}
}
希望你们能让我走上正确的道路!
这段话与IT技术无关,可能需要更多上下文信息才能提供更准确的翻译。