public Page<TodoResponse> getTodos(int page, int size) {
Pageable pageable = PageRequest.of(page - 1, size);
Page<Todo> todos = todoRepository.findAllByOrderByModifiedAtDesc(pageable);
return todos.map(todo -> new TodoResponse(
todo.getId(),
todo.getTitle(),
todo.getContents(),
todo.getWeather(),
new UserResponse(todo.getUser().getId(), todo.getUser().getEmail()),
todo.getCreatedAt(),
todo.getModifiedAt()
));
}
작동 되는지 테스트코드 짜기!
@Test
public void Todo_페이지_레파지토리에서_뽑아오기(){
//given
int page = 1;
int size = 10;
User user = new User();
Pageable pageable = PageRequest.of(page,size);
List<Todo> todos = List.of(new Todo("title","contents","sunny",user));
Page<Todo> todoss = new PageImpl<>(todos,pageable,1);
given(todoRepository.findAllByOrderByModifiedAtDesc(any(Pageable.class))).willReturn(todoss);
//when
Page<TodoResponse> result = todoService.getTodos(page,size).map(a->new TodoResponse(
a.getId(),
a.getTitle(),
a.getContents(),
a.getWeather(),
new UserResponse(a.getUser().getId(), a.getUser().getEmail()),
a.getCreatedAt(),
a.getModifiedAt()
));
//then
assertNotNull(result);
}
의문1. given부분에서 willReturn에 todoss를 만들어서 실제 빈 값을 넣어줘야되는가..? any로 표현되는 아무 값이 들어가야 되는 것이 아닌가..?
의문2. 그렇다면 todo, todos, todoss 값들을 실제로 정의해줘야 되는가...?
'Spring' 카테고리의 다른 글
Spring Batch란 ?? (1) | 2024.10.26 |
---|---|
SET의 위험성! (0) | 2024.09.13 |
웹개발 필수지식 정리 (1) | 2024.08.28 |
JWT (0) | 2024.08.21 |
Annotation (0) | 2024.08.20 |