JUnit テストを実行して、例外をスローするメソッドをテストしようとしています。ただし、テストは失敗し、なぜ失敗したのかわかりません。例外をスローするメソッドは、calcultor.setN( ); です。このテストの 2 つのバージョンを作成しましたが、合格するはずなのに両方とも失敗します。
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testSetNZero() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Het aantal CPU's is minder dan 1");
Amdahl calculator = new Amdahl();
calculator.setN(0);
fail("Exception not thrown");
}
@Test (expected = IllegalArgumentException.class)
public void testSetNZero() {
Amdahl calculator = new Amdahl();
calculator.setN(0);
}
アムダール級:
public class Amdahl
{
private int N;
public void setN (int n) {
if(n < 1) throw new IllegalArgumentException ("Het aantal CPU's is minder dan 1");
this.N = n;
}
}