私はJUNIT 4を使用しています。以下のコードを使用して例外をアサートしようとしましたが、動作しませんでした。JUNIT - 例外をアサートする
@Rule
public ExpectedException thrown = ExpectedException.none();
thrown.expect(ApplicationException.class);
ただし、以下のように注釈を付けると効果があり、テストに合格しました。
@Test(expected=ApplicationException.class)
何か不足している場合は教えてください。
import org.junit.Rule;
import org.junit.rules.ExpectedException;
public class Test
{
@Rule
public ExpectedException exception = ExpectedException.none();
@org.junit.Test
public void throwsIllegalArgumentExceptionIfIconIsNull()
{
exception.expect(IllegalArgumentException.class);
toTest();
}
private void toTest()
{
throw new IllegalArgumentException();
}
}
あなたは[mcve]を投稿できますか? – Tunaki
申し訳ありませんが、わかりやすい完全な例はデバッグが簡単だったはずです。 –