1
我正在使用Maven,並嘗試使用Junit進行測試。Maven測試和例外測試註釋
當我要聲明一個測試,檢查是否有異常拋出,目前我這樣做:
@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說異常已被拋出並且測試失敗:/
我正在使用JU NIT 4.12在我的pom.xml
編輯:實例
我的測試類:
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
您能否提供Maven顯示的確切消息? – beatngu13
我用一個例子編輯了我的帖子:) – graille