[Spring Boot] 17. 스프링부트 상점 v1 (8) JUnit을 사용한 단위테스트

김주희's avatar
Mar 23, 2025
[Spring Boot] 17. 스프링부트 상점 v1 (8) JUnit을 사용한 단위테스트

1. 테스트 폴더 구조

notion image
 

2.

1.@Test : 단위 테스트 단위
 
 
조회가 잘 되는지 ㅌㅅㅌ
package com.metacoding.storev1.log; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.Import; @Import(LogRepository.class) @DataJpaTest // em IoC 등록 public class LogRepositoryTest { @Autowired // DI하는 annotation. 새로운 DI 방법 (annotation으로 주입. 디폴트 생성자로 new되는데 ...) private LogRepository logRepository; // 이것만 있으면 아직 null임 @Test // 세모 버튼 생김 public void findAllJoinStore_test(){ // _test = 컨벤션 // 매개변수에 아무것도 적을 수 없다. logRepository.findAllJoinStore(); } }
@Repository // IoC public class LogRepository { private EntityManager em; // DI public LogRepository(EntityManager em) { this.em = em; } public void findAllJoinStore(){ String q = "SELECT lt.id, st.name, lt.qty, lt.total_price, lt.buyer FROM log_tb lt INNER JOIN store_tb st ON lt.store_id = st.id"; Query query = em.createNativeQuery(q); List<Object[]> obsList = (List<Object[]>) query.getResultList(); // Object[] -> ROW ([id, name, qyt, total_price, buyer]) for(Object[] obs : obsList){ System.out.print(obs[0]); System.out.print(obs[1]); System.out.print(obs[2]); System.out.print(obs[3]); System.out.print(obs[4]); System.out.println("================="); } } }
notion image
 
 
 
 
 
 
 
 
package com.metacoding.storev1.log; import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.Import; import com.metacoding.storev1.log.LogResponse.ListPage; @Import(LogRepository.class) @DataJpaTest // em IoC 등록 public class LogRepositoryTest { @Autowired // DI하는 annotation. 새로운 DI 방법 (annotation으로 주입. 디폴트 생성자로 new되는데 ...) private LogRepository logRepository; // 이것만 있으면 아직 null임 @Test // 세모 버튼 생김 public void findAllJoinStore_test(){ // _test = 컨벤션 // 매개변수에 아무것도 적을 수 없다. List<LogResponse.ListPage> logList = logRepository.findAllJoinStore(); for (ListPage listPage : logList){ System.out.println(listPage); } } }
 
 
 
 
잘 담겼는지 ㅌㅅㅌ
 
notion image
 
 
order by 쿼리 추가 ㅌㅅㅌ
notion image
 
Share article