test code with mysql & redis testcontainers

This commit is contained in:
jinho jeong
2022-04-27 15:18:44 +09:00
parent e468379dd0
commit 6d13be0c9e
4 changed files with 64 additions and 9 deletions

View File

@@ -36,7 +36,10 @@ dependencies {
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok' 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' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }

View File

@@ -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

View File

@@ -16,19 +16,20 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; 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.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession; 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.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.filter.CharacterEncodingFilter;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
@SpringBootTest @SpringBootTest
@Transactional @ActiveProfiles("test")
@AutoConfigureMockMvc
public class UserControllerTest { public class UserControllerTest {
private MockMvc mvc; private MockMvc mvc;
@Autowired @Autowired
@@ -42,6 +43,15 @@ public class UserControllerTest {
.build(); .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){ private UserEntity createTestUser(String username, String password){
return userService.signUp(UserEntity.builder() return userService.signUp(UserEntity.builder()
.username(username) .username(username)
@@ -60,7 +70,7 @@ public class UserControllerTest {
String json = new ObjectMapper().registerModule(new JavaTimeModule()).writeValueAsString(requestBody); String json = new ObjectMapper().registerModule(new JavaTimeModule()).writeValueAsString(requestBody);
final ResultActions actions = mvc.perform( final ResultActions actions = mvc.perform(
post("/user/") post("/user/signup/")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.session(httpSession) .session(httpSession)
.accept(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)

View File

@@ -7,15 +7,18 @@ import com.example.oneul.domain.user.dto.LoginDTO;
import com.example.oneul.domain.user.service.UserService; import com.example.oneul.domain.user.service.UserService;
import org.junit.jupiter.api.Test; 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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpSession; import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.crypto.password.PasswordEncoder; 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 @SpringBootTest
@ExtendWith(MockitoExtension.class) @ActiveProfiles("test")
public class UserCommandServiceTest { public class UserCommandServiceTest {
@Autowired @Autowired
private UserService userCommandService; private UserService userCommandService;
@@ -23,6 +26,15 @@ public class UserCommandServiceTest {
private PasswordEncoder passwordEncoder; private PasswordEncoder passwordEncoder;
protected MockHttpSession httpSession; 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 @Test
public void signUpTest(){ public void signUpTest(){
LoginDTO loginDTO = new LoginDTO("zzzinho", "password"); LoginDTO loginDTO = new LoginDTO("zzzinho", "password");