본문 바로가기

Backend/SpringBoot

[Spring Boot] main이외 디렉토리 추가 및 적용하기

통합 테스트를 구성하는 과정에 내용을 main 외에 별도의 디렉토리에 구성하는 사항을 해결하고자 합니다.

 

maven에서는 org.codehaus.mojo:build-helper-maven-plugin 를 적용해서 POM에 디렉토리를 적용할 수 있습니다.

  • source dir
  • resource dir
  • test source dir
  • test resource dir

적용법은 아래와 같습니다.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-integration-test-source</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/integrationTest/java</source>
                </sources>
            </configuration>
        </execution>
        <execution>
            <id>add-integration-test-resource</id>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>add-test-resource</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>src/integrationTest/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Gradle에서는 sourceSets 을 통해 간단하게 해결할 수 있습니다.

plugins {
    id 'org.springframework.boot' version '2.7.5'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

sourceSets {
    test {
        java {
            srcDirs 'src/integrationTest/java'
        }
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.projectlombok:lombok:1.18.20'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    testImplementation 'io.cucumber:cucumber-java8:7.8.1'
    testImplementation 'io.cucumber:cucumber-spring:7.8.1'

}

tasks.named('test') {
    useJUnitPlatform()
}