【问题标题】:Maven test and excepted test annotationMaven 测试和异常测试注解
【发布时间】:2017-10-25 11:48:57
【问题描述】:

我正在使用 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 

【问题讨论】:

    标签: java maven exception junit maven-plugin


    【解决方案1】:

    我找到了问题! 我已经扩展了TestCase

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 2022-01-02
      • 2014-07-07
      • 2011-01-28
      相关资源
      最近更新 更多