我正在使用Maven,我尝试使用Junit进行测试。
当我想声明一个检查是否抛出异常的测试时,我现在这样做:
@Test
public void testAddPlayerWithInvalidBirthdate() throws Exception {
boolean test = false;
try {
this.playerService.add("Jean", "Dupont", new MyCalendar(2010, 12, 21), "test", "test");
} catch (MinorPersonException e) {
test = true;
}
assertTrue(test);
}
此测试效果很好 但是当我试着写这个:
@Test(expected = MinorPersonException.class)
public void testAddPlayerWithInvalidBirthdate() throws Exception {
this.playerService.add("Jean", "Dupont", new MyCalendar(2010, 12, 21), "test", "test");
}
当我用mvn test
处理测试时,maven说抛出异常并且测试失败了:/
我在我的pom.xml中使用JUnit 4.12
编辑:示例
我的测试班:
public class PlayerParametersTest extends TestCase {
@Test(expected = InvalidNameException.class)
public void testInvalidFirstName() throws Exception {
new Player("", "Dupont", new MyCalendar(1980, 03, 17), "test", "test");
}
}
Maven结果:
Results :
Tests in error:
PlayerParametersTest.testInvalidFirstName:14 » InvalidName Invalid firstname
答案 0 :(得分:2)
我发现了这个问题!
我已经延长了TestCase