LoginSignup
42
48

More than 5 years have passed since last update.

JUnitのアノテーションについて

Last updated at Posted at 2016-06-22

JUnit 4からJUnit 5への移行方法はここです。

アノテーション一覧

JUnit公式からリンクのあったLatest JUnit API JavaDocsを参考にした.
つまり,JUnit 4.12におけるアノテーション一覧.

org.junit

Test

public void なメソッドに付与することでJUnitにテストメソッドであることを認識させる.

Example.java
public class Example {
    @Test 
    public void method() {
       org.junit.Assert.assertTrue( new ArrayList().isEmpty() );
    }
 }

expectedオプションをつけることで期待された例外が投げられたかテストできる.

@Test(expected=IndexOutOfBoundsException.class) public void outOfBounds() {
       new ArrayList<Object>().get(1);
}

また,timeout オプションをつけることでテストに制限時間を設定できる.

@Test(timeout=100) public void infinity() {
       while(true);
}

Ignore

付与することで,一時的にテストを無視することができる.

@Ignore @Test public void something() { ...

メッセージをつけることも可能

@Ignore("not ready yet") @Test public void something() { ...

また,メソッドだけでなくクラスに付与することも可能.

@Ignore public class IgnoreMe {
                        @Test public void test1() { ... }
                        @Test public void test2() { ... }
}

Before

pubic void なメソッドに付与することで,各テストメソッドを実行する前に都度実行してくれる.いわゆる setup メソッド

Example.java
public class Example {
    List empty;
    @Before public void initialize() {
       empty= new ArrayList();
    }
    @Test public void size() {
       ...
    }
    @Test public void remove() {
       ...
    }
}

After

pubic void なメソッドに付与することで,各テストメソッドを実行した後に都度実行してくれる.いわゆる teardown メソッド

Example.java
public class Example {
    File output;
    @Before public void createOutputFile() {
          output= new File(...);
    }
    @Test public void something() {
          ...
    }
    @After public void deleteOutputFile() {
          output.delete();
    }
}

BeforeClass

pubic void なメソッドに付与することで,各テストメソッドを実行する前に1度だけ実行してくれる.いわゆる setup メソッド

Example.java
public class Example {
    @BeforeClass public static void onlyOnce() {
       ...
    }
    @Test public void one() {
       ...
    }
    @Test public void two() {
       ...
    }
}

AfterClass

pubic void なメソッドに付与することで,各テストメソッドを実行した前に1度だけ実行してくれる.いわゆる teardown メソッド

Example.java
public class Example {
    DatabaseConnection database;
    @BeforeClass public static void login() {
          database= ...;
    }
    @Test public void something() {
          ...
    }
    @Test public void somethingElse() {
          ...
    }
    @AfterClass public static void logout() {
          database.logout();
    }
}

Rule

ClassRule

FixMethodOrder

org.junit.runner

RunWith

org.junit.runner.notification

RunListener.ThreadSafe

org.junit.runners

Prametarized.Parameter

Prametarized.Parameters

Prametarized.UseParametersRunnerFactory

Suite.SuiteClasses

org.junit.experimental.categories

Category

Categories.ExcludeCategory

Categories.IncludeCategory

org.junit.experimental.theories

DataPoint

DataPoints

FromDataPoints

DataPoint

ParametersSuppliedBy

Theory

org.junit.experimental.theories.suppliers

TestedOn

org.junit.validator

ValidateWith

org.hamcrest

Factory

42
48
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
48