application test : testcontainers - 설치, 실행
This commit is contained in:
@@ -30,9 +30,10 @@
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
@@ -43,6 +44,25 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>1.15.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>1.15.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.apptest;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class Study {
|
||||
|
||||
private StudyStatus status = StudyStatus.DRAFT;
|
||||
|
||||
private int limit;
|
||||
private int limitCount;
|
||||
|
||||
private String name;
|
||||
|
||||
@@ -29,7 +29,7 @@ public class Study {
|
||||
private Long ownerId;
|
||||
|
||||
public Study(int limit, String name) {
|
||||
this.limit = limit;
|
||||
this.limitCount = limit;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class Study {
|
||||
if (limit <= 0) {
|
||||
throw new IllegalArgumentException("limit은 0보다 커야 한다.");
|
||||
}
|
||||
this.limit = limit;
|
||||
this.limitCount = limit;
|
||||
}
|
||||
|
||||
public void open() {
|
||||
|
||||
@@ -25,4 +25,12 @@ public class StudyService {
|
||||
memberService.notify(member);
|
||||
return newStudy;
|
||||
}
|
||||
|
||||
public Study openStudy(Study study) {
|
||||
study.open();
|
||||
Study openedStudy = studyRepository.save(study);
|
||||
memberService.notify(openedStudy);
|
||||
return openedStudy;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
spring:
|
||||
h2:
|
||||
console:
|
||||
enabled: true
|
||||
path: /h2-console
|
||||
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testDb
|
||||
driver-class-name: org.h2.Driver
|
||||
username: sa
|
||||
password:
|
||||
url: jdbc:postgresql://localhost:5432/study
|
||||
username: study
|
||||
password: study
|
||||
|
||||
jpa:
|
||||
hibernate:
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.example.apptest.study;
|
||||
|
||||
import com.example.apptest.domain.Member;
|
||||
import com.example.apptest.domain.Study;
|
||||
import com.example.apptest.member.MemberService;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
@SpringBootTest
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@ActiveProfiles("test")
|
||||
@Testcontainers
|
||||
public class StudyServiceSpringBootTest {
|
||||
|
||||
@Mock MemberService memberService;
|
||||
@Autowired StudyRepository studyRepository;
|
||||
|
||||
@Container
|
||||
static PostgreSQLContainer<?> postgreSQLContainer =
|
||||
new PostgreSQLContainer<>().withDatabaseName("studytest");
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
studyRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_51() {
|
||||
System.out.println("테스트 완료!!");
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.example.apptest.study;
|
||||
|
||||
import com.example.apptest.domain.Member;
|
||||
import com.example.apptest.domain.Study;
|
||||
import com.example.apptest.domain.StudyStatus;
|
||||
import com.example.apptest.member.MemberService;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -22,6 +23,31 @@ import static org.mockito.Mockito.*;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class StudyServiceTest {
|
||||
|
||||
@DisplayName("다른 사용자가 볼 수 있도록 스터디를 공개한다.")
|
||||
@Test
|
||||
void test_50(@Mock MemberService memberService,
|
||||
@Mock StudyRepository studyRepository) {
|
||||
// Given
|
||||
StudyService studyService = new StudyService(memberService, studyRepository);
|
||||
Study study = new Study(10, "더 자바, 테스트");
|
||||
assertNull(study.getOpenedDateTime());
|
||||
// TODO studyRepository Mock 객체의 save 메소드를호출 시 study를 리턴하도록 만들기.
|
||||
given(studyRepository.save(study)).willReturn(study);
|
||||
|
||||
// When
|
||||
studyService.openStudy(study);
|
||||
|
||||
// Then
|
||||
// TODO study의 status가 OPENED로 변경됐는지 확인
|
||||
assertEquals(StudyStatus.OPENED, study.getStatus());
|
||||
// TODO study의 openedDataTime이 null이 아닌지 확인
|
||||
assertNotNull(study.getOpenedDateTime());
|
||||
// TODO memberService의 notify(study)가 호출 됐는지 확인.
|
||||
then(memberService).should().notify(study);
|
||||
|
||||
System.out.println("테스트 완료!");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("BDD style test")
|
||||
void test_49(@Mock MemberService memberService,
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:tc:postgresql:///studytest
|
||||
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
|
||||
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: create-drop
|
||||
Reference in New Issue
Block a user