본문 바로가기

Back-end/테스트9

Spring Boot Rest APIs Test Spring Boot REST APIs 테스트 status code content type JSON response body 를 어떻게 테스트 할 수 있을 까 전체코드 @TestPropertySource("/application-test.properties") @AutoConfigureMockMvc @SpringBootTest @Transactional // JPA Entity Manager 사용 public class GradebookControllerTest { private static MockHttpServletRequest request; @PersistenceContext private EntityManager em; @Mock StudentAndGradeService studentAndGra.. 2022. 11. 14.
Spring Boot MVC 서비스 테스트 전체코드 test/java/com/.../springmvc/StudentAndGradeServiceTest.java @TestPropertySource("/application.properties") @SpringBootTest public class StudentAndGradeServiceTest { @Autowired private StudentAndGradeService studentService; @Autowired private StudentDao studentDao; @Autowired private MathGradesDao mathGradesDao; @Autowired private ScienceGradesDao scienceGradesDao; @Autowired private History.. 2022. 11. 9.
Spring Boot MVC Controller Test (컨트롤러 테스트) 목적 Spring MVC Web Controllers 테스트 HTTP requests 생성과 전송 HTTP response 검증 (status code, view name, model attributes) Process @AutoConfigureMockMvc MockMvc 주입 web requests 수행 Expectations 정의 Assert results 테스트 클래스 package com.luv2code.springmvc; @TestPropertySource("/application.properties") @AutoConfigureMockMvc @SpringBootTest public class GreadeBookControllerTest { @Autowired private JdbcTemplat.. 2022. 11. 5.
Spring Boot MVC 데이터베이스 통합 테스트 @Sql Execution Sequence @BeforeAll (static method) @BeforeEach 2022. 10. 29.
Spring Boot Unit Testing - Mocking with Mockito - @MockBean, ReflectionTestUtils 더블 테스트 Service - DAO - DB - DAO 더블 테스트 목적 DAO 더블 테스트 에서 테스트 후 DAO 실제 세계에 적용 더블 테스트의 기술 : mocking 주어진 클래스를 따로 테스트할 수 있게 해줍니다. 주어진 클래스와 종속성 사이에서 상호작용 Configuration 최소화 , 종속성의 가용성 DAO, DB, REAT API 등 >> Spring Boot는 Mocking 프레임워크 중 Mockito 지원 Mock 테스트 과정 DAO를 위한 Mock 생성 Service 에 Mock 주입 Set up expectations(기대치) Execute test 메소드 Assert results Verify 메소드 호출 @Mock 테스트를 할 때 필요한 실제 객체와 동일한 모의 객체를 만들어 .. 2022. 10. 21.
Spring Boot Unit Testing Support - 1 Maven 종속성 추가 org.springframework.boot spring-boot-starter-test test JUnit 5가 포함되어있어서 자유롭게 사용할 수 있습니다. test 패키지 추가 main 의 패키지와 test 의 패키지 경로가 다르다면 test 클래스에서 @SpringBootTest 애노테이션의 classes 파라미터를 통해 메인의 부트 실행 클래스를 명시 @SpringBootTest(classes = MvcTestingExampleApplication.class) application.properties 파일 읽기 @SpringBootTest(classes = MvcTestingExampleApplication.class) public class ApplicationExampl.. 2022. 10. 10.
TDD 테스트 주도 개발 연습 TDD TDD란 테스트 주도 개발이라고 하며 반복 테스트를 이용한 소프트웨어 방법론으로, 작은 단위 테스트 케이스를 작성하고 이를 통과하는 코드를 추가하는 단계를 반복하여 구현합니다. 리팩토링(유지보수)의 용이성 개발 과정 실패한 테스트 작성 코드가 테스트를 통과할 수 있도록 작성 필요에 따라 코드 리팩토링 프로세스 반복 주제 :Fizz Buzz // 3으로 나누어 지면 Fizz // 5로 나누어 지면 -> Buzz // 3과 5로 나누어 지면 FizzBuzz // 둘다 안나눠지면 숫자 출력 1. 실패한 테스트 코드 작성 @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class FizzBuzzTest { // 1. 실패한 테스트 코드 작성 @Display.. 2022. 10. 5.
JUnit - 코드 커버리지 테스트 테스트 코드커버리지 코드 커버리지는 테스트라고 불리는 것에 얼마나 많은 메소드와 라인이 있는지 측정 커버리지는 퍼센테이지로 표현하고 높은 것이 좋다. IntelliJ 에는 코드 검사 지원 기능이 내장 코드 커버리지 보고서를 생성할 수 있음. 커버리지 테스트 진행 전체 메소드의 90% 사용 라인의 95%가 덮여있습니다. 이 뜻은 테스트하는 클래스의 전체 라인중 95% 만 단위 테스트를 했다는 것을 의미합니다. 커버리지 보고서 생성 또한 HTML 을 통해서 보고서를 세부적으로 볼 수 있습니다. Test Results - DemoUtilsTest 조건 테스트 특정 상황, OS 버전 @Disabled("...") @EnabledOnOs @EnabledOnOs(OS.WINDOWS) @EnabledOnOs({OS... 2022. 9. 25.
JUnit 테스트 - Assertions 테스트 단위 테스트 Automated tests Better code design Fewer bugs and higher reliability Increases confidence for code refactoring DevOps and Build Pipelines CI/CD 통합 테스트 하향식 통합 테스트 깊이 우선, 너비 우선 Stub (가짜 모듈) 사용 상향식 통합 테스트 클러스터 : 하위 모듈 그룹 드라이버 : 더미 모듈 순서 낮은 수준 모듈들을 클러스터로 결합 드라이버 작성 클러스터 검사/테스트 드라이버 제거하고 클러스터를 상위로 결합 유닛테스트 프레임워크 JUnit Mockito Unit Test Process Set Up Execute Assert class DemoUtilsTest { @T.. 2022. 9. 24.