주요글: 도커 시작하기
반응형

스프링 부트 2에서 JUnit 5를 사용하는 방법을 정리한다. 먼저 pom.xml 파일을 다음과 같이 수정한다.


  • spring-boot-starter-test 의존 설정에서 junit:junit을 제외 처리
  • junit-jupiter-api 의존 추가
  • maven-surefire-plugin 플러그인 JUnit 5 기준 설정. 주의할 점은 maven-sufire-plugin의 버전을 2.19.1로 설정해야 한다는 점이다. 스프링 부트 2.0.2는 maven-surefire-plugin의 2.21.0 버전을 기본으로 사용하는데 이 버전은 JUnit 5를 제대로 처리하지 못한다.

다음은 설정 예이다.


<?xml version="1.0" encoding="UTF-8"?>

<project ...>

    <modelVersion>4.0.0</modelVersion>


    <groupId>madvirus</groupId>

    <artifactId>boot2-junit5</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>


    <name>boot2-junit5</name>


    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.2.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>


    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.9</java.version>

        <junit-jupiter.version>5.1.1</junit-jupiter.version>

        <junit-platform.version>1.1.1</junit-platform.version>

    </properties>


    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>


        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-devtools</artifactId>

            <scope>runtime</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

            <exclusions>

                <exclusion>

                    <groupId>junit</groupId>

                    <artifactId>junit</artifactId>

                </exclusion>

            </exclusions>

        </dependency>


        <dependency>

            <groupId>org.junit.jupiter</groupId>

            <artifactId>junit-jupiter-api</artifactId>

        </dependency>

    </dependencies>


    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>


            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.19.1</version>

                <dependencies>

                    <dependency>

                        <groupId>org.junit.platform</groupId>

                        <artifactId>junit-platform-surefire-provider</artifactId>

                        <version>${junit-platform.version}</version>

                    </dependency>

                    <dependency>

                        <groupId>org.junit.jupiter</groupId>

                        <artifactId>junit-jupiter-engine</artifactId>

                        <version>${junit-jupiter.version}</version>

                    </dependency>

                </dependencies>

            </plugin>

        </plugins>

    </build>


</project>


JUnit5를 이용해서 스프링 부트 테스트를 실행하는 예제 코드는 다음과 같다.


package boot2junit5;


import org.assertj.core.api.Assertions;

import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit.jupiter.SpringExtension;


@ExtendWith(SpringExtension.class)

@SpringBootTest

public class ApplicationTest {


    @Autowired

    private HelloService helloService;


    @Test

    void hello() {

        Assertions.assertThat(helloService.hello("안녕")).isEqualTo("안녕");

    }


}


@ExtendWith 애노테이션은 JUnit5에서 확장 기능을 실행할 때 사용한다. SpringExtension은 JUnit5를 위한 스프링 확장 기능으로 스프링 연동 테스트를 실행할 수 있게 한다.


관련 링크


+ Recent posts