publicclassTaskServiceTest {
@MockTodoValidatortodoValidator;
@MockTodoRepotodoRepository;
privateTodoServicetaskService;
@CaptorArgumentCaptor<TodoItem> todoItemArgumentCaptor;
@BeforeEachvoidsetUp() {
MockitoAnnotations.openMocks(this);
taskService = newTodoService(todoValidator, todoRepository);
}
@TestvoiditShouldAddNewTodoTask() {
// givenStringtitle = "Interview Test";
TodoItemtodoItem = newTodoItem(title, LocalDate.now(), "some desription", Status.PENDING);
given(todoRepository.findByTitle(title)).willReturn(Optional.empty());
given(todoValidator.validateTitleLength(title)).willReturn(true);
// whentaskService.addTask(todoItem);
// thenthen(todoRepository).should().save(todoItemArgumentCaptor.capture());
TodoItemtodoItemCaptor = todoItemArgumentCaptor.getValue();
assertThat(todoItemCaptor).isEqualTo(todoItem);
}
}
@BeforeEachpublicvoidsetup() {
// We would need this line if we would not use the MockitoExtension// MockitoAnnotations.initMocks(this);// Here we can't use @AutoConfigureJsonTesters because there isn't a Spring contextJacksonTester.initFields(this, newObjectMapper());
// MockMvc standalone approachmockMvc = MockMvcBuilders.standaloneSetup(testController)
.build();
}