728x90
참고 : 2022.05.26 - [TIL] - 253일차(모험 162일차) - test 코드 작성해보기
253일차(모험 162일차) - test
test - application.properties
spring.thymeleaf.prefix=classpath:/static/
# H2 Console 사용하도록 변경
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
# sql 보기 좋게 변경
spring.jpa.properties.hibernate.format_sql=true
# 해당하는 테이블이 있으면 DROP하고 새로 만들어 버린다.
#spring.jpa.hibernate.ddl-auto=create
test 1. 기존 db로 확인하기(단순 출력)
package com.dalcho.adme.service;
import com.dalcho.adme.repository.RegistryRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.*;
import java.util.List;
import java.util.Random;
//import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional
public class RegistryPagingTest {
// db를 불러와서 curpage가 x일때 보여지는 페이지
@Autowired
RegistryRepository registryRepository;
@Autowired
RegistryService registryService;
@DisplayName("MySql을 이용한 TEST")
@Test
void paging() throws Exception {
int curPage = 1;
int count = registryService.getBoards(curPage).getCount(); // 총 페이지 수
int start = registryService.getBoards(curPage).getStartPage(); // 시작 번호
int end = registryService.getBoards(curPage).getEndPage(); // 끝 번호
boolean prev = registryService.getBoards(curPage).isPrev();
boolean next = registryService.getBoards(curPage).isNext();
if(end >= count) {
end = count;
next = false;
}
if (prev) {
assertThat(start).isGreaterThan(5);
}else{
assertThat(start).isLessThan(6);
}
if (next) {
assertThat(count).isGreaterThan(5);
} else {
assertThat(count).isLessThan(6);
}
}
}
*1L과 같이 long 타입은 L을 붙여줘야 한다.
1이라는 값은 저장하기 전에 임시로 메모리에 저장되는데
이 때 사용되는 기본 데이터 타입이 int라서 에러가 뜬다. 그래서 소문자나 대문자 L을 붙여줘야한다.
출처 : long,float 타입 오류 : 왜 리터럴 뒤에 L,F 을 붙여야 할까?
test 2. H2 로 TEST 하기
@DisplayName("H2를 이용한 TEST")
@TestPropertySource(locations = "/application.properties")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PagingTest {
@Autowired
RegistryRepository registryRepository;
@Autowired
RegistryService registryService;
@Test
@DisplayName("실제 로직이 잘 동작하는지 test")
public void paging() {
Registry registry = registryRepository.save(new Registry(1L,"HI","TEST","TEST"));
Registry registry1 = registryRepository.save(new Registry(2L,"HI","TEST","TEST"));
Registry registry2 = registryRepository.save(new Registry(3L,"HI","TEST","TEST"));
Registry registry3 = registryRepository.save(new Registry(4L,"HI","TEST","TEST"));
Registry registry4 = registryRepository.save(new Registry(5L,"HI","TEST","TEST"));
Registry registry5 = registryRepository.save(new Registry(6L,"HI","TEST","TEST"));
int curPage = 1;
Pageable pageable = PageRequest.of(curPage - 1, 5);
Page<Registry> boards = registryRepository.findAllByOrderByCreatedAtDesc(pageable);
System.out.println("전체 데이터 = " + registryService.getBoards(1).getData());
System.out.println("시작 페이지 = " + registryService.getBoards(1).getStartPage());
System.out.println("끝 페이지 = " + registryService.getBoards(1).getEndPage());
System.out.println("전체 페이지 수 = " + boards.getTotalPages());
Assertions.assertThat(boards.getNumber() + 1).isEqualTo(curPage);
Assertions.assertThat(registryService.getBoards(1).getStartPage()).isEqualTo(1);
Assertions.assertThat(registryService.getBoards(1).getEndPage()).isEqualTo(1);
Assertions.assertThat(registryService.getBoards(1).isPrev()).isEqualTo(false);
Assertions.assertThat(registryService.getBoards(1).isNext()).isEqualTo(false);
}
@Test
@DisplayName("size를 바꿔도 실행이 잘 되는지 test")
public void paging2() {
int curPage = 1;
int startPage;
int endPage;
boolean prev;
boolean next;
int nowPage;
int displayPageNum=5;
Registry registry = registryRepository.save(new Registry(1L,"HI","TEST","TEST"));
Registry registry1 = registryRepository.save(new Registry(2L,"HI","TEST","TEST"));
Registry registry2 = registryRepository.save(new Registry(3L,"HI","TEST","TEST"));
Registry registry3 = registryRepository.save(new Registry(4L,"HI","TEST","TEST"));
Registry registry4 = registryRepository.save(new Registry(5L,"HI","TEST","TEST"));
Registry registry5 = registryRepository.save(new Registry(6L,"HI","TEST","TEST"));
Pageable pageable = PageRequest.of(curPage-1, 1);
Page<Registry> boards = registryRepository.findAllByOrderByCreatedAtDesc(pageable);// 생성 날짜 순으로 보여주기
nowPage = (boards.getNumber()+1);
startPage = ( ((int) Math.ceil(nowPage/ (double) displayPageNum )) -1) *5 +1;
endPage = startPage+4;
prev = startPage == 1 ? false : true;
next = endPage < boards.getTotalPages()? true : false;
if(endPage >= boards.getTotalPages()) {
endPage = boards.getTotalPages();
next = false;
}
Assertions.assertThat(startPage).isEqualTo(1);
Assertions.assertThat(endPage).isEqualTo(5);
Assertions.assertThat(prev).isEqualTo(false);
Assertions.assertThat(next).isEqualTo(true);
}
}
728x90
'Project' 카테고리의 다른 글
웹소켓 - 상대방과 나 구분하기 (0) | 2022.06.04 |
---|---|
웹소켓 기본 코드 (0) | 2022.06.03 |
Registry 코드 정리 - front (+ 페이징) (0) | 2022.05.25 |
Registry 코드 정리 - back (+ 페이징) (0) | 2022.05.25 |
웹 소켓 코드 정리 (0) | 2022.03.30 |