JUnit 설치 가이드

JUnit는 Maven, Gradle, 직접 JAR 다운로드 등 다양한 방식으로 프로젝트에 추가할 수 있습니다. 아래에는 각 버전별 설치 방법이 자세히 설명되어 있습니다.

JUnit 5 설치

Maven 설정

pom.xml에 다음 의존성을 추가합니다:

<!-- JUnit Jupiter 애그리게이터 모듈 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.14.3</version>
    <scope>test</scope>
</dependency>

Maven Surefire Plugin을 사용하여 테스트를 실행하는 경우, 다음 설정도 추가합니다:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.5.2</version>
</plugin>

Gradle 설정

build.gradle에 의존성을 추가합니다:

testImplementation 'org.junit.jupiter:junit-jupiter:5.14.3'

테스트 실행:

test {
    useJUnitPlatform()
}

직접 JAR 다운로드

Maven Central에서 필요한 JAR 파일을 다운로드합니다:

  • junit-jupiter-api: JUnit Jupiter API
  • junit-jupiter-engine: JUnit Jupiter 엔진
  • junit-platform-console-standalone: 독립형 콘솔 런처

JUnit 4 설치

Maven 설정

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

Gradle 설정

testImplementation 'junit:junit:4.13.2'

직접 JAR 다운로드

Maven Central에서 junit-4.13.2.jar을 다운로드합니다:

https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar

JUnit Vintage 설치

JUnit 5 플랫폼에서 JUnit 4 테스트를 실행하려면 Vintage 엔진 의존성을 추가합니다:

Maven

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.14.3</version>
    <scope>test</scope>
</dependency>

Gradle

testImplementation 'org.junit.vintage:junit-vintage-engine:5.14.3'

버전 호환성

  • JUnit 5는 Java 8 이상이 필요합니다
  • JUnit 4는 Java 5 이상이 필요합니다
  • JUnit Vintage 5.14.3은 JUnit 4.12 이상이 필요합니다

설치 확인

테스트 클래스를 생성하여 설치를 확인합니다:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class SampleTest {
    @Test
    void testExample() {
        assertEquals(2, 1 + 1);
    }
}

테스트 실행:

mvn test
# 또는
gradle test