Skip to content

Instantly share code, notes, and snippets.

@b1a9id
Created January 16, 2018 10:45
Show Gist options
  • Save b1a9id/9c4437ded9d8c3b50159e4c26abeb1c2 to your computer and use it in GitHub Desktop.
Save b1a9id/9c4437ded9d8c3b50159e4c26abeb1c2 to your computer and use it in GitHub Desktop.
class PageWrapperTest {
private PageWrapper pageWrapper;
@Mock
private Page page;
private Method initPaginationMethod;
private int totalPages;
@BeforeEach
void beforeEach() {
initMocks(this);
}
@Nested
class PaginationSize_5 {
private int paginationSize = 5;
@DisplayName("トータルページ数がページネーション数より大きいとき")
@TestFactory
Stream<DynamicTest> InitPaginationPaginationSizeGreaterThanTotalPage() {
totalPages = 10;
Iterator<Integer> inputGenerator = IntStream.range(0, totalPages).iterator();
Function<Integer, String> displayNameGenerator = (currentPageNumber) -> "currentPageNumber: " + currentPageNumber;
ThrowingConsumer<Integer> testExecutor = (currentPageNumber) ->
Assertions.assertAll(() -> {
initPaginationMethod = PageWrapper.class.getDeclaredMethod("initPagination", int.class);
initPaginationMethod.setAccessible(true);
when(page.getNumber()).thenReturn(currentPageNumber);
when(page.getTotalPages()).thenReturn(totalPages);
pageWrapper = new PageWrapper(page, paginationSize);
Integer[] showPages = getShowPages(currentPageNumber);
assertThat(pageWrapper.getShowPages())
.containsOnly(showPages);
});
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
@DisplayName("トータルページ数がページネーション数より小さいとき")
@TestFactory
Stream<DynamicTest> InitPaginationPaginationSizeLessThanTotalPage() {
totalPages = 4;
Iterator<Integer> inputGenerator = IntStream.range(0, totalPages).iterator();
Function<Integer, String> displayNameGenerator = (currentPageNumber) -> "currentPageNumber: " + currentPageNumber;
ThrowingConsumer<Integer> testExecutor = (currentPageNumber) ->
Assertions.assertAll(() -> {
initPaginationMethod = PageWrapper.class.getDeclaredMethod("initPagination", int.class);
initPaginationMethod.setAccessible(true);
when(page.getNumber()).thenReturn(currentPageNumber);
when(page.getTotalPages()).thenReturn(totalPages);
pageWrapper = new PageWrapper(page, paginationSize);
Integer[] showPages = new Integer[] {0, 1, 2, 3, 4};
assertThat(pageWrapper.getShowPages())
.containsOnly(showPages);
});
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
private Integer[] getShowPages(int currentPageNumber) {
List<Integer[]> showPages = new ArrayList<>();
showPages.add(new Integer[] {0, 1, 2, 3, 4});
showPages.add(new Integer[] {0, 1, 2, 3, 4});
showPages.add(new Integer[] {0, 1, 2, 3, 4});
showPages.add(new Integer[] {1, 2, 3, 4, 5});
showPages.add(new Integer[] {2, 3, 4, 5, 6});
showPages.add(new Integer[] {3, 4, 5, 6, 7});
showPages.add(new Integer[] {4, 5, 6, 7, 8});
showPages.add(new Integer[] {5, 6, 7, 8, 9});
showPages.add(new Integer[] {5, 6, 7, 8, 9});
showPages.add(new Integer[] {5, 6, 7, 8, 9});
return showPages.get(currentPageNumber);
}
}
@Nested
class PaginationSize_6 {
private int paginationSize = 6;
@DisplayName("トータルページ数がページネーション数より大きいとき")
@TestFactory
Stream<DynamicTest> InitPaginationPaginationSizeGreaterThanTotalPage() {
totalPages = 10;
Iterator<Integer> inputGenerator = IntStream.range(0, totalPages).iterator();
Function<Integer, String> displayNameGenerator = (currentPageNumber) -> "currentPageNumber: " + currentPageNumber;
ThrowingConsumer<Integer> testExecutor = (currentPageNumber) ->
Assertions.assertAll(() -> {
initPaginationMethod = PageWrapper.class.getDeclaredMethod("initPagination", int.class);
initPaginationMethod.setAccessible(true);
totalPages = 10;
when(page.getNumber()).thenReturn(currentPageNumber);
when(page.getTotalPages()).thenReturn(totalPages);
pageWrapper = new PageWrapper(page, paginationSize);
Integer[] showPages = getShowPages(currentPageNumber);
assertThat(pageWrapper.getShowPages())
.containsOnly(showPages);
});
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
@DisplayName("トータルページ数がページネーション数より小さいとき")
@TestFactory
Stream<DynamicTest> InitPaginationPaginationSizeLessThanTotalPage() {
totalPages = 4;
Iterator<Integer> inputGenerator = IntStream.range(0, totalPages).iterator();
Function<Integer, String> displayNameGenerator = (currentPageNumber) -> "currentPageNumber: " + currentPageNumber;
ThrowingConsumer<Integer> testExecutor = (currentPageNumber) ->
Assertions.assertAll(() -> {
initPaginationMethod = PageWrapper.class.getDeclaredMethod("initPagination", int.class);
initPaginationMethod.setAccessible(true);
totalPages = 4;
when(page.getNumber()).thenReturn(currentPageNumber);
when(page.getTotalPages()).thenReturn(totalPages);
pageWrapper = new PageWrapper(page, paginationSize);
Integer[] showPages = new Integer[] {0, 1, 2, 3, 4, 5};
assertThat(pageWrapper.getShowPages())
.containsOnly(showPages);
});
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
private Integer[] getShowPages(int currentPageNumber) {
List<Integer[]> showPages = new ArrayList<>();
showPages.add(new Integer[] {0, 1, 2, 3, 4, 5});
showPages.add(new Integer[] {0, 1, 2, 3, 4, 5});
showPages.add(new Integer[] {0, 1, 2, 3, 4, 5});
showPages.add(new Integer[] {1, 2, 3, 4, 5, 6});
showPages.add(new Integer[] {2, 3, 4, 5, 6, 7});
showPages.add(new Integer[] {3, 4, 5, 6, 7, 8});
showPages.add(new Integer[] {4, 5, 6, 7, 8, 9});
showPages.add(new Integer[] {4, 5, 6, 7, 8, 9});
showPages.add(new Integer[] {4, 5, 6, 7, 8, 9});
showPages.add(new Integer[] {4, 5, 6, 7, 8, 9});
return showPages.get(currentPageNumber);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment