@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
public abstract class AbstractRestAssuredTest {
@LocalServerPort
int port;
@Autowired
private MemberRepository memberRepository;
@BeforeEach
void setUp() {
RestAssured.port = port;
}
@AfterEach
void tearDown() {
memberRepository.deleteAllInBatch();
}
@Autowired
protected ResourceLoader resourceLoader;
}
- MemberRepository의 deleteAllInBatch를 통해 멤버 테이블을 초기화 시켜줌
→ beforeAll()
, beforeAllInBatch()
차이
// 데이터 베이스 초기화
@Component
@Profile("local || default")
public class DatabaseCleaner implements InitializingBean {
@PersistenceContext
private EntityManager entityManager;
private static final List<String> deletionExclusionList = List.of(
"GROUP_CODE",
"CODE"
);
private List<String> tableNames;
@Override
public void afterPropertiesSet() {
tableNames = entityManager.getMetamodel().getEntities().stream()
.map(entityType -> entityType.getJavaType().getDeclaredAnnotation(Table.class))
.filter(tableMetaData -> tableMetaData != null
&& !deletionExclusionList.contains(tableMetaData.name()))
.map(Table::name)
.toList();
}
@Transactional
public void cleanUp() {
entityManager.flush();
// 참조 무결성 제약 조건 임시 해제
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
// 모든 테이블 삭제
for (String tableName : tableNames) {
entityManager.createNativeQuery("TRUNCATE TABLE " + tableName).executeUpdate();
}
// 참조 무결성 제약 조건 활성화
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate();
}
}
@Profile("local || default")
@TestPropertySource(properties = {
"aws.s3.teamImageBucket=wemeet-test-bucket",
"aws.s3.profileImageBucket=wemeet-test-bucket"
})
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public abstract class AbstractAcceptanceTest {
@LocalServerPort
int port;
@Autowired
private DatabaseCleaner databaseCleaner;
@BeforeEach
void setUp() {
RestAssured.port = port;
}
@AfterEach
void tearDown() {
databaseCleaner.cleanUp();
}
@Autowired
protected ResourceLoader resourceLoader;
}