JUnit如何处理DateTimeParseException

时间:2018-03-30 13:02:14

标签: java exception junit

如何测试错误的输入'使用JUnit?我试图通过将一个无法解析的值传递给LocalTime来实例化Flight对象。当我这样做时,"失败"在JUnit中,testConstructor方法给出了一个错误。我应该如何处理此异常,以便它可以无错误地通过JUnit测试?谢谢

protected Flight(String scheduledTime, String eventType, String identifier) {
    try {
        this.scheduledTime = LocalTime.parse(scheduledTime);
        this.eventType = EventType.valueOf(eventType);
        this.identifier = identifier;
        this.actualTime = null;
        this.runwayUsed = null;
    }
    catch(Exception e) {
        System.out.println(e + " Flight constructor");
    } 

} //end of constructor

下面的try / catch块是给出错误的JUnit代码。

@Test
public void testConstructor() {

    Flight f1 = new Flight("00:00", "ARRIVAL", "A001");
    Flight f2 = new Flight("00:00", "DEPARTURE", "D001");

    assertEquals(LocalTime.parse("00:00"), f1.getScheduledTime());
    assertEquals(EventType.ARRIVAL, f1.getEvent());
    assertEquals("A001", f1.getIdent());
    assertEquals(null, f1.getActualTime());
    assertEquals(null, f1.getRunwayUsed());

    assertEquals(LocalTime.parse("00:00"), f2.getScheduledTime());
    assertEquals(EventType.DEPARTURE, f2.getEvent());
    assertEquals("D001", f2.getIdent());
    assertEquals(null, f2.getActualTime());
    assertEquals(null, f2.getRunwayUsed());

    //invalid entry for scheduledTime
    try {
        Flight f3 = new Flight("00:0j", "ARRIVAL", "A001");
        fail("Expected exception");
    } //end of try
    catch(Exception e) {
        System.out.println(e);
    } //end of catch

} 

4 个答案:

答案 0 :(得分:1)

首先,正如@binoternary所指出的那样,你的构造函数不会在异常之外抛出异常而只是记录它。 其次,要仅在抛出特定异常时才使测试通过,则需要将此批注添加到测试方法中:

@Test(expected = DateTimeParseException.class) // or any other exception class expected to be thrown
public void testException() {
    Flight f3 = new Flight("00:0j", "ARRIVAL", "A001");
}

答案 1 :(得分:1)

不要在构造函数中捕获Exception

protected Flight(final String scheduledTime,
                 final String eventType,
                 final String identifier) {

    this.scheduledTime = LocalTime.parse(scheduledTime);
    this.eventType = EventType.valueOf(eventType);
    this.identifier = identifier;
    this.actualTime = null;
    this.runwayUsed = null;
}

更好的设计可能是将构造函数更改为:

protected Flight(final LocalTime scheduledTime,
                 final EventType eventType,
                 final String identifier) {
    ...
}

此外,我建议将您的单项测试分解为三个独立的,有名的测试。

祝你好运。

答案 2 :(得分:0)

你正在做的是正确的,但是如果你使用Junit 4可能会更清楚你可以在 @Test 注释中指定你期望的例外,请参阅here细节。

如果测试失败,那是因为你的Flight构造函数没有抛出任何异常。

答案 3 :(得分:0)

你有一些选择。

第一个是在构造函数方法中捕获异常。在这种情况下,您应该调整您的测试方法。 因此,构造函数将类似于:

protected Flight(String scheduledTime, String eventType, String identifier) {
        this.identifier = identifier;
        this.actualTime = null;
        this.runwayUsed = null;    
    try {
            this.scheduledTime = LocalTime.parse(scheduledTime);
        } catch(Exception e) {
            System.out.println(e + " Flight constructor - scheduledTime problem");
        } 
    try {
             this.eventType = EventType.valueOf(eventType);
        } catch(Exception e) {
            System.out.println(e + " Flight constructor - eventType problem");
        }
}

因此,您的测试应该是:

@Test
public void testConstructor() {
    Flight f = new Flight("00:0j", "ARRIVAL", "A001");
    assertNull(f.scheduledTime);
    assertEquals("A001", f.identifier); 
    // other asserts
}

第二个选项是在构造函数中出现错误时抛出异常。在这种情况下,构造函数将是:

protected Flight(String scheduledTime, String eventType, String identifier) {
    this.identifier = identifier;
        this.actualTime = null;
        this.runwayUsed = null;    
        this.scheduledTime = LocalTime.parse(scheduledTime);
        this.eventType = EventType.valueOf(eventType);      
}

你的测试可能就像:

 @Test
   public void testConstructor() {

    try {
          new Flight("00:0j", "ARRIVAL", "A001");
          fail("Expected exception");
    } catch(Exception e) {
           assertNotNull(e);
        } 
}

@Test(expected = Exception.class) // You can also specialize this exception to DateTimeParseException or any other
public void testConstructor() { 
     new Flight("00:0j", "ARRIVAL", "A001");

}

如您所见,您有几种选择。