//Test code
class Topic_Service_test
{
@Mock
private Topic_Repository topic_repository;
@InjectMocks
private Topic_Service topic_service = Mockito.spy(new Topic_Service());
@Test
void get_topic_test()
{
Topic_Table topic = new Topic_Table(2,"java");
Mockito.when(topic_repository.findById(Mockito.anyInt()))
.thenReturn(Optional.of(topic));
Mockito.doReturn(topic).when(topic_service.get_topic(2));//.thenReturn(topic);
Assertions.assertEquals(topic,topic_service.get_topic(2));
}
}
class Topic_Service
{
@Autowired
private Topic_Repository topic_repository;
public Topic_Table get_topic(int id)
{
System.out.println("get topic exec");
return topic_repository.findById(id).orElse(new Topic_Table());
}
}
По мне, topic_service.get_topi c (2); в классе Topic_Service_test должен возвращать объект в соответствии с Mockito.doReturn (topi c). when (topic_service.get_topi c (2)); но на самом деле он показывает какую-то ошибку, которую я не получаю.
ОШИБКА ПОКАЗЫВАЕТ: - org.mockito.exceptions.misusing.UnfinishedStubbingException: Незавершенная заглушка обнаружена здесь: -> в io.practice.practice_course_api. Topic_Service_test.get_topic_test (Topic_Service_test. java: 53)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if
completed