Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 변환
- 보험
- 급성심근경색증
- PythonProgramming
- 가입
- 특약
- 중도인출
- jQuery
- 납입
- 교보
- 코딩
- 자바스크립트
- 추가납입
- 심장질환
- 문자열
- 인출수수료
- 프론트엔드
- 웹개발
- 보험료
- Vue.js
- 사망
- javascript
- 교보생명
- 뇌출혈
- Java
- 프로그래밍
- 리스트
- python
- 파이썬
- 수수료
Archives
- Today
- Total
SeouliteLab
[Java/자바] Spring @SpringBootTest: 스프링 부트 애플리케이션 통합 테스트 본문
스프링 부트에서는 @SpringBootTest 어노테이션을 사용하여 애플리케이션의 전체 컨텍스트를 로드하고 통합 테스트를 수행할 수 있습니다. 이를 통해 실제 환경과 유사한 테스트 환경에서 애플리케이션을 테스트할 수 있습니다. 이번 글에서는 @SpringBootTest 어노테이션의 사용법과 예제 코드를 통해 자세히 살펴보겠습니다.
1. 기본적인 @SpringBootTest 사용법
가장 간단한 형태의 @SpringBootTest 어노테이션은 테스트 클래스에 적용됩니다. 이 경우에는 스프링 부트 애플리케이션 전체를 로드하고 테스트를 수행합니다.
예제 1: 기본적인 @SpringBootTest 사용법
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringBootTest
public class MyIntegrationTest {
@Autowired
private MyService myService;
@Test
public void testService() {
assertNotNull(myService);
// 테스트할 내용 추가
}
}
위의 예제에서는 MyService라는 서비스를 테스트합니다. @SpringBootTest 어노테이션을 사용하여 애플리케이션 컨텍스트를 로드하고, @Autowired를 통해 필요한 빈을 주입받아 테스트를 수행합니다.
2. 특정 프로파일을 사용하는 @SpringBootTest
특정 프로파일을 사용하여 애플리케이션을 테스트하려면 properties 속성을 사용하여 프로파일을 지정할 수 있습니다.
예제 2: 특정 프로파일을 사용하는 @SpringBootTest
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringBootTest(properties = "spring.profiles.active=test")
public class MyIntegrationTest {
@Autowired
private MyService myService;
@Test
public void testService() {
assertNotNull(myService);
// 테스트할 내용 추가
}
}
위의 예제에서는 test
프로파일을 사용하여 애플리케이션을 테스트합니다.
3. 예제 코드
다음은 @SpringBootTest 어노테이션을 사용한 예제 코드입니다.
예제 1: 기본적인 @SpringBootTest 사용법
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringBootTest
public class MyIntegrationTest {
@Autowired
private MyService myService;
@Test
public void testService() {
assertNotNull(myService);
// 테스트할 내용 추가
}
}
예제 2: 특정 프로파일을 사용하는 @SpringBootTest
import
org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringBootTest(properties = "spring.profiles.active=test")
public class MyIntegrationTest {
@Autowired
private MyService myService;
@Test
public void testService() {
assertNotNull(myService);
// 테스트할 내용 추가
}
}
4. 결론
이상으로 Spring의 @SpringBootTest 어노테이션에 대한 자세한 설명과 예제 코드를 살펴보았습니다. @SpringBootTest 어노테이션을 사용하여 스프링 부트 애플리케이션을 통합 테스트할 수 있습니다.
'프로그래밍' 카테고리의 다른 글
SSL(Secure Sockets Layer) 이해하기: 데이터 보안의 핵심 (0) | 2024.03.12 |
---|---|
[Java/자바] Lombok의 대표적인 Annotation과 역할 (0) | 2024.03.12 |
[Java/자바] Spring @ResponseBody: HTTP 응답 데이터 직접 제어하기 (0) | 2024.03.12 |
[Java/자바] Spring @ModelAttribute: 메서드 파라미터와 모델 속성의 바인딩 (0) | 2024.03.12 |
[Java/자바] Spring @RequestBody: HTTP 요청의 본문을 메서드 파라미터로 받기 (0) | 2024.03.12 |