diff --git a/application-test/app-test/pom.xml b/application-test/app-test/pom.xml index 3b1eb24f..e6d23bb4 100644 --- a/application-test/app-test/pom.xml +++ b/application-test/app-test/pom.xml @@ -30,9 +30,10 @@ spring-boot-starter-data-jpa - com.h2database - h2 + org.postgresql + postgresql + org.projectlombok lombok @@ -43,6 +44,25 @@ spring-boot-starter-test test + + com.h2database + h2 + test + + + org.testcontainers + junit-jupiter + 1.15.2 + test + + + org.testcontainers + postgresql + 1.15.2 + test + + + org.junit.vintage diff --git a/application-test/app-test/src/main/java/com/example/apptest/App.java b/application-test/app-test/src/main/java/com/example/apptest/App.java new file mode 100644 index 00000000..f0848458 --- /dev/null +++ b/application-test/app-test/src/main/java/com/example/apptest/App.java @@ -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); + } +} diff --git a/application-test/app-test/src/main/java/com/example/apptest/domain/Study.java b/application-test/app-test/src/main/java/com/example/apptest/domain/Study.java index 8984f0bb..2f9c88b9 100644 --- a/application-test/app-test/src/main/java/com/example/apptest/domain/Study.java +++ b/application-test/app-test/src/main/java/com/example/apptest/domain/Study.java @@ -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() { diff --git a/application-test/app-test/src/main/java/com/example/apptest/study/StudyService.java b/application-test/app-test/src/main/java/com/example/apptest/study/StudyService.java index f46f74c1..a756af24 100644 --- a/application-test/app-test/src/main/java/com/example/apptest/study/StudyService.java +++ b/application-test/app-test/src/main/java/com/example/apptest/study/StudyService.java @@ -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; + } + } diff --git a/application-test/app-test/src/main/resources/application.yml b/application-test/app-test/src/main/resources/application.yml index 53372abf..22eeb9ad 100644 --- a/application-test/app-test/src/main/resources/application.yml +++ b/application-test/app-test/src/main/resources/application.yml @@ -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: diff --git a/application-test/app-test/src/test/java/com/example/apptest/study/StudyServiceSpringBootTest.java b/application-test/app-test/src/test/java/com/example/apptest/study/StudyServiceSpringBootTest.java new file mode 100644 index 00000000..f1f45d04 --- /dev/null +++ b/application-test/app-test/src/test/java/com/example/apptest/study/StudyServiceSpringBootTest.java @@ -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("테스트 완료!!"); + } +} diff --git a/application-test/app-test/src/test/java/com/example/apptest/study/StudyServiceTest.java b/application-test/app-test/src/test/java/com/example/apptest/study/StudyServiceTest.java index 616e4558..cdf663c3 100644 --- a/application-test/app-test/src/test/java/com/example/apptest/study/StudyServiceTest.java +++ b/application-test/app-test/src/test/java/com/example/apptest/study/StudyServiceTest.java @@ -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, diff --git a/application-test/app-test/src/test/resources/application-test.yml b/application-test/app-test/src/test/resources/application-test.yml new file mode 100644 index 00000000..5975c10d --- /dev/null +++ b/application-test/app-test/src/test/resources/application-test.yml @@ -0,0 +1,8 @@ +spring: + datasource: + url: jdbc:tc:postgresql:///studytest + driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver + + jpa: + hibernate: + ddl-auto: create-drop \ No newline at end of file