Это часть класса, который я хочу протестировать.
Код: Выделить всё
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 getAllSkeletons() {
try {
TypedQuery 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();
}
}
Аргумент, передаваемый в метод проверки(), имеет тип 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 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();
}
}
Подробнее здесь: https://stackoverflow.com/questions/505 ... not-a-mock
Мобильная версия