Skip to content

JUnit

Install

상호 운용성 문제를 방지하려면 Maven Surefire/Failsafe 3.0.0-M4 이상을 사용하세요.

Maven

종속성을 관리하는 자체적인 방법을 정의하는 Spring Boot를 사용하지 않는 한, JUnit 플랫폼 BOM을 사용하여 모든 JUnit 5 아티팩트의 버전을 정렬하는 것이 좋습니다.

아래는 <dependencyManagement> 이다. <dependencies> 가 아니다.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.11.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

테스트 엔진 구성

Maven Surefire 또는 Maven Failsafe가 테스트를 실행하려면 최소한 하나의 TestEngine 구현을 테스트 클래스 경로에 추가해야 합니다.

JUnit Jupiter 기반 테스트에 대한 지원을 구성하려면 다음과 유사하게 JUnit Jupiter API와 JUnit Jupiter TestEngine 구현에서 테스트 범위 종속성을 구성합니다.

<!-- ... -->
<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.11.3</version> <!-- can be omitted when using the BOM -->
        <scope>test</scope>
    </dependency>
    <!-- ... -->
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.3.1</version>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.3.1</version>
        </plugin>
    </plugins>
</build>
<!-- ... -->

Maven Surefire와 Maven Failsafe는 다음과 유사하게 JUnit 4와 JUnit Vintage TestEngine 구현에서 테스트 범위 종속성을 구성하는 한 Jupiter 테스트와 함께 JUnit 4 기반 테스트를 실행할 수 있습니다.

<!-- ... -->
<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.11.3</version> <!-- can be omitted when using the BOM -->
        <scope>test</scope>
    </dependency>
    <!-- ... -->
</dependencies>
<!-- ... -->
<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.3.1</version>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.3.1</version>
        </plugin>
    </plugins>
</build>
<!-- ... -->

Simple example

import static org.junit.jupiter.api.Assertions.assertEquals;
import example.util.Calculator;
import org.junit.jupiter.api.Test;

class MyFirstJUnitJupiterTests {
    private final Calculator calculator = new Calculator();

    @Test
    void addition() {
        assertEquals(2, calculator.add(1, 1));
    }
}

JUnit 5

JUnit 5는 이전 버전들과 다르게 3개의 서브 프로젝트 모듈로 이루어져있습니다.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
  • JUnit Platform : TestEngine API, Console Launcher, JUnit 4 based Runner 등 포함
  • JUnit Jupiter : TestEngine API 구현체로 JUnit 5 구현
  • JUnit Vintage : TestEngine API 구현체로 JUnit 3, 4 구현

JUnit 5에서 JUnit Vintage 모듈을 포함하고 있어 JUnit 3, 4 문법을 사용할 수 있습니다. 하지만 완벽하게 지원해주는 것이 아니기 때문에 만약 사용한다하면 추가로 작업이 필요합니다.

Functions

기본 테스트 함수:

  • assertEquals(expected, actual)

Annotations

What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations.

This blog post explains it best. Basically, it is the following:

  • Small: this test doesn't interact with any file system or network.
  • Medium: Accesses file systems on box which is running tests.
  • Large: Accesses external file systems, networks, etc.

Feature

Small

Medium

Large

Network access

No

localhost only

Yes

Database

No

Yes

Yes

File system access

No

Yes

Yes

Use external systems

No

Discouraged

Yes

Multiple threads

No

Yes

Yes

Sleep statements

No

Yes

Yes

System properties

No

Yes

Yes

Time limit (seconds)

60

300

900+

intellij에서 JUnit 설정 및 간단한 테스트 코드 작성

See also

Favorite site

References


  1. Unit_Testing_with_JUnit_-_Tutorial.pdf 

  2. How_to_use_Eclipse_JUnit.pdf