From 6d13be0c9e5223438b81f953edd7d5cc752db3bb Mon Sep 17 00:00:00 2001 From: jinho jeong Date: Wed, 27 Apr 2022 15:18:44 +0900 Subject: [PATCH] test code with mysql & redis testcontainers --- build.gradle | 5 +++- src/main/resources/application-test.yml | 30 +++++++++++++++++++ .../oneul/controller/UserControllerTest.java | 20 +++++++++---- .../oneul/service/UserCommandServiceTest.java | 18 +++++++++-- 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 src/main/resources/application-test.yml diff --git a/build.gradle b/build.gradle index 9c8bf1a..08cc7c0 100644 --- a/build.gradle +++ b/build.gradle @@ -36,7 +36,10 @@ dependencies { compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' - testImplementation "org.testcontainers:junit-jupiter:1.16.3" + + testImplementation "org.testcontainers:testcontainers:1.15.3" + testImplementation "org.testcontainers:junit-jupiter:1.16.3" + testImplementation "org.testcontainers:mysql:1.15.3" testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml new file mode 100644 index 0000000..3f6f49e --- /dev/null +++ b/src/main/resources/application-test.yml @@ -0,0 +1,30 @@ +spring: + config: + activate: + on-profile: test + jpa: + properties: + hibernate: + show-sql: true + ddl-auto: "update" + hbm2ddl: + auto: update + format_sql: true + datasource: + url: jdbc:tc:mysql://localhost:3306/oneul + driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver + redis: + host: localhost + port: 6379 + +server: + servlet: + session: + timeout: 60 + +logging: + level: + web: DEBUG + org: + hibernate: + SQL: DEBUG \ No newline at end of file diff --git a/src/test/java/com/example/oneul/controller/UserControllerTest.java b/src/test/java/com/example/oneul/controller/UserControllerTest.java index d31b96d..7a0b5ac 100644 --- a/src/test/java/com/example/oneul/controller/UserControllerTest.java +++ b/src/test/java/com/example/oneul/controller/UserControllerTest.java @@ -16,19 +16,20 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.transaction.annotation.Transactional; import org.springframework.web.filter.CharacterEncodingFilter; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.junit.jupiter.Testcontainers; +@Testcontainers @SpringBootTest -@Transactional -@AutoConfigureMockMvc +@ActiveProfiles("test") public class UserControllerTest { private MockMvc mvc; @Autowired @@ -42,6 +43,15 @@ public class UserControllerTest { .build(); } + static { + GenericContainer redis = new GenericContainer("redis:3-alpine") + .withExposedPorts(6379); + redis.start(); + + System.setProperty("spring.redis.host", redis.getContainerIpAddress()); + System.setProperty("spring.redis.port", redis.getFirstMappedPort() + ""); + } + private UserEntity createTestUser(String username, String password){ return userService.signUp(UserEntity.builder() .username(username) @@ -60,7 +70,7 @@ public class UserControllerTest { String json = new ObjectMapper().registerModule(new JavaTimeModule()).writeValueAsString(requestBody); final ResultActions actions = mvc.perform( - post("/user/") + post("/user/signup/") .contentType(MediaType.APPLICATION_JSON) .session(httpSession) .accept(MediaType.APPLICATION_JSON) diff --git a/src/test/java/com/example/oneul/service/UserCommandServiceTest.java b/src/test/java/com/example/oneul/service/UserCommandServiceTest.java index 3ee99b4..12d4499 100644 --- a/src/test/java/com/example/oneul/service/UserCommandServiceTest.java +++ b/src/test/java/com/example/oneul/service/UserCommandServiceTest.java @@ -7,15 +7,18 @@ import com.example.oneul.domain.user.dto.LoginDTO; import com.example.oneul.domain.user.service.UserService; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mock.web.MockHttpSession; import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.test.context.ActiveProfiles; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.junit.jupiter.Testcontainers; + +@Testcontainers @SpringBootTest -@ExtendWith(MockitoExtension.class) +@ActiveProfiles("test") public class UserCommandServiceTest { @Autowired private UserService userCommandService; @@ -23,6 +26,15 @@ public class UserCommandServiceTest { private PasswordEncoder passwordEncoder; protected MockHttpSession httpSession; + static { + GenericContainer redis = new GenericContainer("redis:3-alpine") + .withExposedPorts(6379); + redis.start(); + + System.setProperty("spring.redis.host", redis.getContainerIpAddress()); + System.setProperty("spring.redis.port", redis.getFirstMappedPort() + ""); + } + @Test public void signUpTest(){ LoginDTO loginDTO = new LoginDTO("zzzinho", "password");