Java Maven测试和例外测试注释
我正在使用Maven,并尝试使用Junit进行测试 当我想要声明一个检查是否引发异常的测试时,目前我执行以下操作:Java Maven测试和例外测试注释,java,maven,exception,junit,maven-plugin,Java,Maven,Exception,Junit,Maven Plugin,我正在使用Maven,并尝试使用Junit进行测试 当我想要声明一个检查是否引发异常的测试时,目前我执行以下操作: @Test public void testAddPlayerWithInvalidBirthdate() throws Exception { boolean test = false; try { this.playerService.add("Jean", "Dupont", new MyCalendar(2010, 12, 21), "te
@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中使用JUnit4.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
我发现了问题!
我已经扩展了TestCase