我正在尝试对其他人编写的代码进行 JUnit 测试,但我无法弄清楚如何测试异常,因为异常似乎缺少类型。
public Pirate(String name, int initialGold) throws Exception {
if(initialGold < 0)
throw new Exception("Init Gold must be >= 0");
this.name = name;
this.numGold = initialGold;
this.health = Pirate.DEFAULT_HEALTH;
this.isCursed = false;
}
我的 JUnit 代码片段:
@Test
public static void constructorTest() throws Exception{
rodgers = new Pirate("Dread Pirate Rodgers", 10000);
assertEquals("Dread Pirate Rodgers" , rodgers.getName());
assertEquals(10000, rodgers.getNumGold());
assertEquals(100, rodgers.getHealth());
assertEquals(false, rodgers.getIsCursed());
}
@Test()
public static void exceptionTest() throws Exception{
rodgers = new Pirate("Dread Pirate Rodgers" , -100);
}
我知道我需要将预期 =(某种类型的异常)放在 test 的括号中,但我对异常类型一无所知。