티스토리 뷰

Mock Test만으로는 db의 작동을 잘 검증하기 어려울 때 h2를 이용하면 수월합니다.

 

버전

Java 17

SpringBoot 3.0.0

 

build.gradle

특이사항은 testImplementation com.h2database:h2 입니다. test에서 h2를 쓰겠다는 뜻입니다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'

    implementation 'io.jsonwebtoken:jjwt:0.9.1'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'

    testImplementation 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation group: 'org.springframework.security', name: 'spring-security-test'
}

 

application.yml

test쪽에 별도의 .yml을 만들어서 넣어야 합니다.

 

datasource쪽에 h2설정이 들어가 있습니다.

 

application.yml

server:
  servlet:
    encoding:
      force-response: true
spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1
    username: sa
    password:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  h2:
    console:
      enabled: true

logging:
  level:
    org:
      hibernate:
        SQL: DEBUG

 

TestCode

@ExtendWith, @DataJpaTest를 사용합니다.

@ExtendWith(SpringExtension.class)
@DataJpaTest
public class ForDevelop {
    @Autowired
    LikeRepository likeRepository;

    @Autowired
    PostRepository postRepository;
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함