-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
테스트 코드 작성 환경 설정
- Loading branch information
Showing
4 changed files
with
152 additions
and
15 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/test/java/com/project/bumawiki/domain/docs/service/CommandDocsServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.project.bumawiki.domain.docs.service; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.project.bumawiki.domain.docs.domain.Docs; | ||
import com.project.bumawiki.domain.docs.domain.VersionDocs; | ||
import com.project.bumawiki.domain.docs.domain.repository.DocsRepository; | ||
import com.project.bumawiki.domain.docs.domain.repository.VersionDocsRepository; | ||
import com.project.bumawiki.domain.docs.domain.type.DocsType; | ||
import com.project.bumawiki.domain.user.domain.User; | ||
import com.project.bumawiki.domain.user.domain.authority.Authority; | ||
import com.project.bumawiki.domain.user.domain.repository.UserRepository; | ||
import com.project.bumawiki.global.annotation.ServiceTest; | ||
|
||
@ServiceTest | ||
class CommandDocsServiceTest { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
@Autowired | ||
private CommandDocsService commandDocsService; | ||
@Autowired | ||
private DocsRepository docsRepository; | ||
@Autowired | ||
private VersionDocsRepository versionDocsRepository; | ||
|
||
@Test | ||
void 문서_생성하기() { | ||
// given | ||
User user = User.builder() | ||
.email("[email protected]") | ||
.name("마현우") | ||
.enroll(2022) | ||
.nickName("마현우") | ||
.authority(Authority.USER) | ||
.build(); | ||
userRepository.save(user); | ||
|
||
String title = "제목"; | ||
String contents = "본문"; | ||
int enroll = 2024; | ||
Docs docs = new Docs(title, enroll, DocsType.ACCIDENT); | ||
|
||
// when | ||
commandDocsService.create(docs, user, contents); | ||
|
||
// then | ||
assertThat(docsRepository.findByTitle(title).get().getTitle()).isEqualTo(title); | ||
assertThat(versionDocsRepository.findFirstByDocsOrderByVersionDesc(docs).getContents()).isEqualTo(contents); | ||
} | ||
|
||
@Test | ||
void 문서_업데이트하기() { | ||
// given | ||
User user = User.builder() | ||
.email("[email protected]") | ||
.name("마현우") | ||
.enroll(2022) | ||
.nickName("마현우") | ||
.authority(Authority.USER) | ||
.build(); | ||
userRepository.save(user); | ||
|
||
String title = "제목"; | ||
String contents = "본문"; | ||
String updatedContents = "본문본문"; | ||
int enroll = 2024; | ||
Docs docs = new Docs(title, enroll, DocsType.ACCIDENT); | ||
commandDocsService.create(docs, user, contents); | ||
|
||
// when | ||
commandDocsService.update(user, title, updatedContents, 0); | ||
|
||
// then | ||
VersionDocs versionDocs = versionDocsRepository.findFirstByDocsOrderByVersionDesc(docs); | ||
assertThat(versionDocs.getContents()).isEqualTo(updatedContents); | ||
assertThat(versionDocs.getVersion()).isEqualTo(1); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/project/bumawiki/global/annotation/ServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.project.bumawiki.global.annotation; | ||
|
||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
import static java.lang.annotation.RetentionPolicy.*; | ||
|
||
@Target(TYPE) | ||
@Retention(RUNTIME) | ||
@Transactional | ||
@SpringBootTest | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
public @interface ServiceTest { | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/com/project/bumawiki/global/truncate/Truncate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.project.bumawiki.global.truncate; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class Truncate { | ||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Transactional | ||
public void beforeEach() { | ||
final List<String> truncateQueries = getTruncateQueries(jdbcTemplate); | ||
execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY FALSE"); | ||
truncateTables(jdbcTemplate, truncateQueries); | ||
execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY TRUE"); | ||
} | ||
|
||
private List<String> getTruncateQueries(final JdbcTemplate jdbcTemplate) { | ||
return jdbcTemplate.queryForList("SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ' RESTART IDENTITY;') AS q FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'", String.class); | ||
} | ||
|
||
private void truncateTables(final JdbcTemplate jdbcTemplate, final List<String> truncateQueries) { | ||
truncateQueries.forEach(v -> execute(jdbcTemplate, v)); | ||
} | ||
|
||
private void execute(final JdbcTemplate jdbcTemplate, final String query) { | ||
jdbcTemplate.execute(query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters