Skip to content

Commit

Permalink
Chore | #4 | @lcomment | TestContainer 및 테스트 환경 구축
Browse files Browse the repository at this point in the history
  • Loading branch information
lcomment committed May 9, 2024
1 parent 4c08aa4 commit b08834f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.cakk.api.common.base;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import org.junit.jupiter.api.BeforeEach;
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.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.api.introspector.BuilderArbitraryIntrospector;
import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector;
import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector;

@AutoConfigureMockMvc
@ActiveProfiles("test")
@SpringBootTest
public abstract class ControllerTest {

@Autowired
protected MockMvc mockMvc;

@Autowired
protected ObjectMapper objectMapper;

@BeforeEach
void setup(WebApplicationContext webApplicationContext) {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilters(new CharacterEncodingFilter("UTF-8", true))
.alwaysDo(print())
.build();
}

protected final FixtureMonkey getConstructorMonkey() {
return FixtureMonkey.builder()
.objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
.build();
}

protected final FixtureMonkey getReflectionMonkey() {
return FixtureMonkey.builder()
.objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
.build();
}

protected final FixtureMonkey getBuilderMonkey() {
return FixtureMonkey.builder()
.objectIntrospector(BuilderArbitraryIntrospector.INSTANCE)
.build();
}
}
37 changes: 37 additions & 0 deletions cakk-api/src/test/java/com/cakk/api/common/base/ServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.cakk.api.common.base;

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;

import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.api.introspector.BuilderArbitraryIntrospector;
import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector;
import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector;

import com.cakk.domain.config.JpaConfig;

@Import(JpaConfig.class)
@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
public abstract class ServiceTest {

protected final FixtureMonkey getConstructorMonkey() {
return FixtureMonkey.builder()
.objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
.build();
}

protected final FixtureMonkey getReflectionMonkey() {
return FixtureMonkey.builder()
.objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
.build();
}

protected final FixtureMonkey getBuilderMonkey() {
return FixtureMonkey.builder()
.objectIntrospector(BuilderArbitraryIntrospector.INSTANCE)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.cakk.api.common.container;

import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
public abstract class MysqlTestContainer {

private static final String MYSQL_VERSION = "mysql:8";
private static final String DATABASE_NAME = "cakk";

@Container
public static final MySQLContainer MYSQL_CONTAINER = new MySQLContainer(MYSQL_VERSION)
.withDatabaseName(DATABASE_NAME);

static {
MYSQL_CONTAINER.start();
}
}
26 changes: 17 additions & 9 deletions cakk-api/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
spring:
datasource:
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
url: jdbc:tc:mysql:8.0:///cakk
#spring:
# datasource:
# driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
# url: jdbc:tc:mysql:8.0:///cakk
#
# jpa:
# hibernate:
# ddl-auto: create
# show-sql: true

jpa:
hibernate:
ddl-auto: create
show-sql: true
oauth:
kakao:
public-key-info: https://kauth.kakao.com/.well-known/jwks.json
apple:
public-key-url: https://appleid.apple.com/auth/keys
google:
client-id: clientId

storage:
datasource:
core:
jdbc-url: jdbc:tc:mysql:8.0:///cakk
jdbc-url: jdbc:tc:mysql:8.0://cakk
username: username
password: password
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
Expand Down

0 comments on commit b08834f

Please sign in to comment.