Bank System,2022年1月6日


spring data jpaでページングするときのテスト
@Test
    void 전체_계좌_조회() {
        int page = 0;
        int perPage = 5;
        Pageable pageable = PageRequest.of(page, perPage);
        List<Account> accountList = new ArrayList<>();
        accountList.add(account);
        Page<Account> accounts = new PageImpl<>(accountList);

        // given
        when(userService.getUser(request)).thenReturn(user);
        when(accountRepository.findByUser(user, pageable)).thenReturn(accounts);

        // when
        accountService.getAccounts(page, perPage, request);

        // then
        verify(userService).getUser(request);
        verify(accountRepository).findByUser(user, PageRequest.of(page, perPage));
    }
springdatajpaでページングしている場合は、上記の操作を実行します.リストはNew PageImpl<>()でPageオブジェクト(?)として使用されます.作成して使えばいいです.