JUnit ユーザーマニュアル

このマニュアルでは、JUnit 4とJUnit 5の使用方法について説明します。テストの記述、アサーション、テストスイートなどの主要機能をカバーします。

JUnit 5の基本的な使い方

テストクラスの作成

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

class CalculatorTest {
    
    @Test
    void testAddition() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}

一般的なアサーション

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

// 等価アサーション
assertEquals(expected, actual);
assertEquals(expected, actual, "カスタムエラーメッセージ");

// ブールアサーション
assertTrue(condition);
assertFalse(condition);

// Nullアサーション
assertNull(object);
assertNotNull(object);

// 配列アサーション
assertArrayEquals(expectedArray, actualArray);

// 例外アサーション
assertThrows(Exception.class, () -> {
    // 例外を投げる可能性のあるコード
});

// 複合アサーション
assertAll("Person properties",
    () -> assertEquals("John", person.getFirstName()),
    () -> assertEquals("Doe", person.getLastName())
);

前処理と後処理

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;

class MyTest {
    
    @BeforeEach
    void setUp() {
        // 各テストメソッドの前に実行
    }
    
    @AfterEach
    void tearDown() {
        // 各テストメソッドの後に実行
    }
}

ネストされたテスト

import org.junit.jupiter.api.Nested;

@Nested
class NestedTests {
    
    @Test
    void testInNestedClass() {
        // ネストされたテストクラスのテスト
    }
}

パラメータ化テスト

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
void testWithMultipleValues(int number) {
    assertTrue(number > 0);
}

動的テスト

import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.DynamicTest;
import java.util.Arrays;

@TestFactory
Collection<DynamicTest> dynamicTestsFromIterator() {
    return Arrays.asList(
        DynamicTest.dynamicTest("最初の動的テスト", () -> assertTrue(true)),
        DynamicTest.dynamicTest("2番目の動的テスト", () -> assertEquals(4, 2 + 2))
    );
}

JUnit 4の基本的な使い方

テストクラスの作成

import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest {
    
    @Test
    public void testAddition() {
        assertEquals(5, 2 + 3);
    }
}

一般的なアサーション

// 等価アサーション
assertEquals(expected, actual);
assertEquals("メッセージ", expected, actual);

// ブールアサーション
assertTrue(condition);
assertFalse(condition);

// Nullアサーション
assertNull(object);
assertNotNull(object);

// 例外アサーション
@Test(expected = NullPointerException.class)
public void testException() {
    Object obj = null;
    obj.toString();
}

前処理と後処理

import org.junit.Before;
import org.junit.After;

public class MyTest {
    
    @Before
    public void setUp() {
        // 各テストメソッドの前に実行
    }
    
    @After
    public void tearDown() {
        // 各テストメソッドの後に実行
    }
    
    @BeforeClass
    public static void setUpClass() {
        // テストクラス全体で1回実行
    }
    
    @AfterClass
    public static void tearDownClass() {
        // テストクラス完了時に1回実行
    }
}

テストスイート

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class, Test3.class})
public class AllTests {
    // Test1、Test2、Test3のすべてのテストを実行
}

JUnit 4からJUnit 5への移行

アノテーションの変更

JUnit 4JUnit 5
@Test@Test
@Before@BeforeEach
@After@AfterEach
@BeforeClass@BeforeAll
@AfterClass@AfterAll
@Ignore@Disabled
@SuiteClasses@SelectClasses

アサーションの変更

// JUnit 4
Assert.assertEquals(expected, actual);

// JUnit 5
Assertions.assertEquals(expected, actual);

例外テストの変更

// JUnit 4
@Test(expected = NullPointerException.class)
public void testException() {
    Object obj = null;
    obj.toString();
}

// JUnit 5
@Test
public void testException() {
    assertThrows(NullPointerException.class, () -> {
        Object obj = null;
        obj.toString();
    });
}

テストの実行

コマンドライン

# Maven
mvn test

# Gradle
gradle test

# スタンドアロンコンソール
java -jar junit-platform-console-standalone-5.14.3.jar -c MyTestClass

IDEでの実行

  • IntelliJ IDEA:テストクラスまたはメソッドを右クリックしてRunを選択
  • Eclipse:テストクラスまたはメソッドを右クリックしてRun As → JUnit Testを選択
  • VS Code:JUnit拡張機能を使用してテストを実行