简体   繁体   English

Java:Junit4:异常测试不起作用,坚持try-catch块:

[英]Java: Junit4: Exception testing not working, insists on try-catch block:

My Test: this is where it underlines the stuff after sax. 我的测试:这是它强调萨克斯之后的东西。 and insists that I have a try-catch block.... but the internet says that the proper way to test for exception is @Test(expected=IllegalArgumentException.class) 并且坚持认为我有一个try-catch块....但互联网上说测试异常的正确方法是@Test(expected=IllegalArgumentException.class)

@Test(expected= XMLClientNotFoind.class)
public void testGetClientFromIP() throws XMLClientNotFound{
    ...
    assertEquals(c, sax.getClientFromIP("101.0.2.01"));
}

And the method, getClientFromIP is here: 方法getClientFromIP在这里:

  public Client getClientFromIP(String ip) throws XMLClientNotFound {
        ...
        throw new XMLClientNotFound();
  }

And my exception: 我的例外:

   public class XMLClientNotFound extends Exception{

    }

First of all: 首先:

@Test(expected=IllegalArgumentException.class)

should not be considered as a proper way, especially with such a generic exception. 不应被视为一种正确的方式,特别是对于这样的一般例外。 The reason is that you have no control over which statement in your test method actually threw the exception. 原因是您无法控制测试方法中的哪个语句实际抛出异常。 Also you can't make any assertions on the message label, cause, etc. 你也不能在消息标签,原因等上做任何断言。

Using try-catch precisely surrounding a line that is suppose to throw an exception is the right way to go: 使用try-catch精确地围绕一条假定要抛出异常的行是正确的方法:

try {
    shouldThrow()
    fail("Expected exception");
} catch(XMLClientNotFound e) {
    assertThat(e).hasMessage("Expected message");  //FEST-Assert syntax
}

You might also try JUnit @Rule that I wrote some time ago to make your test more readable. 您也可以尝试我前一段时间写的 JUnit @Rule ,以使您的测试更具可读性。

You still need to define throws clause for checked exceptions. 您仍然需要为已检查的异常定义throws子句。 @Test(expected=...) part just says JUnit that you expect your test case to throw that exception(s). @Test(expected=...)部分只是说JUnit,你希望你的测试用例抛出那个异常。

Is it possible you have other code in the test method that throws a different exception? 是否有可能在测试方法中有其他代码抛出不同的异常?

For example... 例如...

@Test(expected= XMLClientNotFoind.class)
public void testGetClientFromIP() throws XMLClientNotFound{

    thisMethodThrows_ExceptionX();

    assertEquals(c, sax.getClientFromIP("101.0.2.01"));
} 

In the above case the compiler would complain because you are not handling ExceptionX. 在上面的例子中,编译器会抱怨,因为你没有处理ExceptionX。 You would either have to surround with try/catch or say throws ExceptionX in test method signature as well. 您可能需要使用try / catch包围或者在测试方法签名中throws ExceptionX

In general it is a good idea to test one thing in a test method. 通常,在测试方法中测试一件事是个好主意。 I do not understand the assertion if you are expecting the method to throw an exception; 如果您期望该方法抛出异常,我不理解断言; there is nothing to assert since it is not going to return anything. 没有什么可以断言的,因为它不会返回任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM