简体   繁体   English

JUnit4 @Test(expected = MyException.class)VS try / catch

[英]JUnit4 @Test(expected=MyException.class) VS try/catch

I'm pondering on exception handling and unit tests best practices because we're trying to get some code best practices in place. 我正在考虑异常处理和单元测试最佳实践,因为我们正在努力获得一些代码最佳实践。

A previous article regarding best practices, found on our company wiki, stated "Do not use try/catch, but use Junit4 @Test(expect=MyException.class)", without further information. 我们公司维基上发现的前一篇关于最佳实践的文章指出“不要使用try / catch,而是使用Junit4 @Test(expect = MyException.class)”,没有进一步的信息。 I'm not convinced. 我不相信。

Many of our custom exception have an Enum in order to identify the failure cause. 我们的许多自定义异常都有一个枚举,以便识别失败原因。 As a result, I would rather see a test like : 结果,我宁愿看到如下的测试:

@Test
public void testDoSomethingFailsBecauseZzz() {
try{
   doSomething();
} catch(OurCustomException e){
   assertEquals("Omg it failed, but not like we planned", FailureEnum.ZZZ, e.getFailure());
}
}

than : 比:

@Test(expected = OurCustomException.class)
public void testDoSomethingFailsBecauseZzz() {
   doSomething();
}

when doSomethig() looks like : 当doSomethig()看起来像:

public void doSomething throws OurCustomException {
  if(Aaa) {
     throw OurCustomException(FailureEnum.AAA);
  } 
  if(Zzz) {
     throw OurCustomException(FailureEnum.ZZZ);
  }
  // ...
}

On a side note, I am more than convinced that on some cases @Test(expected=blabla.class) IS the best choice (for example when the exception is precise and there can be no doubt about what's causing it). 在旁注中,我更确信在某些情况下@Test(expected = blabla.class)是最佳选择(例如,当异常是精确的并且毫无疑问导致它的原因时)。

Am I missing something here or should I push the use of try/catch when necessary ? 我在这里遗漏了什么,还是应该在必要时推动使用try / catch?

It sounds like your enum is being used as an alternative to an exception hierarchy? 听起来你的枚举被用作异常层次结构的替代品? Perhaps if you had an exception hierarchy the @Test(expected=XYZ.class) would become more useful? 也许如果你有一个异常层次结构, @Test(expected=XYZ.class)会变得更有用吗?

  • If you simply want to check that an exception of a certain type was thrown, use the annotation's expected property. 如果您只想检查是否抛出了某种类型的异常,请使用注释的expected属性。
  • If you want to check properties of the thrown exception (eg the message, or a custom member value), catch it in the test and make assertions. 如果要检查抛出异常的属性(例如消息或自定义成员值),请在测试中捕获它并进行断言。

In your case, it seems like you want the latter (to assert that the exception has a certain FailureEnum value); 在你的情况下,似乎你想要后者(断言异常有一定的FailureEnum值); there's nothing wrong with using the try/catch . 使用try/catch没有任何问题

The generalization that you should "not use try/catch" (interpreted as "never") is bunk. 你应该“不使用try / catch”(解释为“从不”)的概括是无聊的。

Jeff is right though; 杰夫是对的; the organization of your exception hierarchy is suspect. 您的异常层次结构的组织是可疑的。 However, you seem to recognize this. 但是,你似乎认识到这一点。 :) :)

If you want to check the raw exception type, then the expected method is appropriate. 如果要检查原始异常类型,则expected方法是合适的。 Otherwise, if you need to test something about the exception (and regardless of the enum weirdness testing the message content is common) you can do the try catch, but that is a bit old-school. 否则,如果你需要测试关于异常的一些东西(并且无论测试消息内容的enum怪异是否常见)你都可以尝试catch,但这有点老了。 The new JUnit way to do it is with a MethodRule . 新的JUnit方法是使用MethodRule The one that comes in the API ( ExpectedException ) is about testing the message specifically, but you can easily look at the code and adapt that implementation to check for failure enum s. API中出现的一个( ExpectedException )是专门测试消息,但是您可以轻松查看代码并调整该实现以检查失败enum

I came across this when searching how to handle exceptions. 我在搜索如何处理异常时遇到了这个问题。

As @Yishai mentioned, the preferred way to expect exceptions is using JUnit rules and ExpectedException . 正如@Yishai所提到的,期望异常的首选方法是使用JUnit规则和ExpectedException

When using @Test(expected=SomeException.class) a test method will pass if the exception is thrown anywhere in the method. 当使用@Test(expected=SomeException.class)如果在方法中的任何位置抛出异常,则测试方法将通过。

When you use ExpectedException : 使用ExpectedException

@Test
public void testException()
{
    // If SomeException is thrown here, the test will fail.
    expectedException.expect(SomeException.class);
    // If SomeException is thrown here, the test will pass.
}

You can also test: 你也可以测试:

  • an expected message: ExpectedException.expectMessage() ; 预期的消息: ExpectedException.expectMessage() ;
  • an expected cause: expectedException.expectCause() . 预期原因: expectedException.expectCause()

As a side note: I don't think using enums for exception messages/causes is good practice. 作为旁注:我不认为使用枚举来处理异常消息/原因是一种很好的做法。 (Please correct me if I'm wrong.) (如果我错了,请纠正我。)

In your special case, you want to test (1) if the expected exception type is thrown and (2) if the error number is correct, because the method can thrown the same exception with different types. 在您的特殊情况下,您希望测试(1)是否抛出预期的异常类型 ,以及(2)如果错误号是正确的,因为该方法可以使用不同的类型抛出相同的异常。

This requires an inspection of the exception object. 这需要检查异常对象。 But, you can stick to the recommendation and verify that the right exception has been thrown: 但是,您可以坚持建议验证是否抛出了正确的异常:

@Test(expected = OurCustomException.class)
public void testDoSomethingFailsBecauseZzz() {
   try {
      doSomething();
   } catch (OurCustomException e) {
      if (e.getFailureEnum.equals(FailureEnum.ZZZ))  // use *your* method here
         throw e;

      fail("Catched OurCostomException with unexpected failure number: " 
        + e.getFailureEnum().getValue());  // again: your enum method here
   }
}

This pattern will eat the unexpected exception and make the test fail. 此模式将 吃掉意外的异常并使测试失败。

Edit 编辑

Changed it because I missed the obvious: we can make a test case fail and capture a message. 改变它是因为我错过了显而易见的事情:我们可以使测试用例失败并捕获消息。 So now: the test passes, if the expected exception with the expected error code is thrown. 所以现在:如果抛出了带有预期错误代码的预期异常,则测试通过。 If the test fails because we got an unexpected error, then we can read the error code. 如果由于我们遇到意外错误而导致测试失败,那么我们就可以读取错误代码。

I made catch-exception because I was facing the same problem as you did, Stph. 我做了一个例外,因为我遇到了和你一样的问题,Stph。 With catch-exception your code could look like this: 使用catch-exception,您的代码可能如下所示:

@Test
public void testDoSomethingFailsBecauseZzz() {
   verifyException(myObj, OurCustomException.class).doSomething();
   assertEquals("Omg it failed, but not like we planned", FailureEnum.ZZZ,    
               ((OurCustomException)caughtException()).getFailure() ;
}

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

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