我正在尝试运行 JUnit 测试来测试会引发异常的方法。但是,测试失败了,我不知道为什么会失败。抛出异常的方法是:calcultor.setN();。我做了这个测试的两个版本,即使它们应该通过,它们都失败了。
@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;
}
}