JUnit Test アノテーションを使用して例外メッセージをアサートするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2469911

質問

私はいくつかの JUnit テストを作成しました @Test 注釈。テストメソッドがチェック例外をスローし、例外とともにメッセージをアサートしたい場合、JUnit を使用してそれを行う方法はありますか @Test 注釈?私の知る限り、JUnit 4.7 はこの機能を提供していませんが、将来のバージョンでは提供されるのでしょうか?.NET ではメッセージと例外クラスをアサートできることは知っています。Java の世界で同様の機能を探しています。

これが私が望むものです:

@Test (expected = RuntimeException.class, message = "Employee ID is null")
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {}
役に立ちましたか?

解決

を使用できます @Rule 注釈付き ExpectedException, 、 このような:

@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Test
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() throws Exception {
    expectedEx.expect(RuntimeException.class);
    expectedEx.expectMessage("Employee ID is null");

    // do something that should throw the exception...
    System.out.println("=======Starting Exception process=======");
    throw new NullPointerException("Employee ID is null");
}

の例に注意してください。 ExpectedException ドキュメントは(現時点では)間違っています - パブリックコンストラクターがないため、使用する必要があります ExpectedException.none().

他のヒント

私は @Rule の答えが好きです。しかし、何らかの理由であなたはルールを使用しない場合。第三の選択肢があります。

@Test (expected = RuntimeException.class)
public void myTestMethod()
{
   try
   {
      //Run exception throwing operation here
   }
   catch(RuntimeException re)
   {
      String message = "Employee ID is null";
      assertEquals(message, re.getMessage());
      throw re;
    }
    fail("Employee Id Null exception did not throw!");
  }

あなたは@Test(expected=SomeException.class)を使用する必要がありますか?私たちは、例外の実際のメッセージを主張する必要がある場合は、これは我々が何をすべきかです。

@Test
public void myTestMethod()
{
  try
  {
    final Integer employeeId = null;
    new Employee(employeeId);
    fail("Should have thrown SomeException but did not!");
  }
  catch( final SomeException e )
  {
    final String msg = "Employee ID is null";
    assertEquals(msg, e.getMessage());
  }
}

あなたが行うことができますJUnitの4.13(一度リリース)ではます:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

...

@Test
void exceptionTesting() {
  IllegalArgumentException exception = assertThrows(
    IllegalArgumentException.class, 
    () -> { throw new IllegalArgumentException("a message"); }
  );

  assertEquals("a message", exception.getMessage());
}

このも JUnitの5 の中で動作しますが、別の輸入でます:

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

...

実際には、最良の使い方はのtry / catchです。どうして?あなたは例外を期待する場所を制御することができますので。

この例を考えてみます:

@Test (expected = RuntimeException.class)
public void someTest() {
   // test preparation
   // actual test
}

どのような一日のコードが変更され、テストの準備がのRuntimeExceptionをスローしますか?その場合、実際の試験でもテストされていないと、それは任意の例外をスローしない場合でも、テストは合格します。

アノテーションに依存するよりも、/キャッチを試みる使用することがはるかに優れている理由を

つまります。

レイストームは良い答えを持っていました。私はどちらかのルールの大ファンではないんです。私が最初の場所での注釈の大きなplus'esの一つで読みやすさと使いやすさを、助けるために、次のユーティリティクラスを作成することを除いて、同様の何かをします。

このユーティリティクラスを追加します:

import org.junit.Assert;

public abstract class ExpectedRuntimeExceptionAsserter {

    private String expectedExceptionMessage;

    public ExpectedRuntimeExceptionAsserter(String expectedExceptionMessage) {
        this.expectedExceptionMessage = expectedExceptionMessage;
    }

    public final void run(){
        try{
            expectException();
            Assert.fail(String.format("Expected a RuntimeException '%s'", expectedExceptionMessage));
        } catch (RuntimeException e){
            Assert.assertEquals("RuntimeException caught, but unexpected message", expectedExceptionMessage, e.getMessage());
        }
    }

    protected abstract void expectException();

}

そして、私のユニットテストのために、私は必要なのは、このコードは次のとおりです。

@Test
public void verifyAnonymousUserCantAccessPrivilegedResourceTest(){
    new ExpectedRuntimeExceptionAsserter("anonymous user can't access privileged resource"){
        @Override
        protected void expectException() {
            throw new RuntimeException("anonymous user can't access privileged resource");
        }
    }.run(); //passes test; expected exception is caught, and this @Test returns normally as "Passed"
}
@Ruleを使用する場合は、

、例外セットは、テストクラス内のすべての試験方法に適用されます。

私はuser64141の答えを好むが、それはより一般化できることを見出しました。ここに私のテイクがあります:

public abstract class ExpectedThrowableAsserter implements Runnable {

    private final Class<? extends Throwable> throwableClass;
    private final String expectedExceptionMessage;

    protected ExpectedThrowableAsserter(Class<? extends Throwable> throwableClass, String expectedExceptionMessage) {
        this.throwableClass = throwableClass;
        this.expectedExceptionMessage = expectedExceptionMessage;
    }

    public final void run() {
        try {
            expectException();
        } catch (Throwable e) {
            assertTrue(String.format("Caught unexpected %s", e.getClass().getSimpleName()), throwableClass.isInstance(e));
            assertEquals(String.format("%s caught, but unexpected message", throwableClass.getSimpleName()), expectedExceptionMessage, e.getMessage());
            return;
        }
        fail(String.format("Expected %s, but no exception was thrown.", throwableClass.getSimpleName()));
    }

    protected abstract void expectException();

}

tryブロック内のステートメントは、関連するアサーション例外がキャッチされるようになり、「失敗」を残すことに注意。 catchステートメント内で使用してリターンがこれを防ぎます。

キャッチ例外のライブラリをインポートし、それを使用しています。それはExpectedExceptionルールやtry-catchよりもはるかにきれいです。

例彼らのドキュメントを形成します:

import static com.googlecode.catchexception.CatchException.*;
import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.*;

// given: an empty list
List myList = new ArrayList();

// when: we try to get the first element of the list
catchException(myList).get(1);

// then: we expect an IndexOutOfBoundsException with message "Index: 1, Size: 0"
assertThat(caughtException(),
  allOf(
    instanceOf(IndexOutOfBoundsException.class),
    hasMessage("Index: 1, Size: 0"),
    hasNoCause()
  )
);
@Test (expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "This is not allowed")
public void testInvalidValidation() throws Exception{
     //test code
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top