test code with mysql & redis testcontainers
This commit is contained in:
@@ -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'
|
||||
}
|
||||
|
||||
|
||||
30
src/main/resources/application-test.yml
Normal file
30
src/main/resources/application-test.yml
Normal 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
|
||||
@@ -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)
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user