【问题标题】:Java: Exception testing with Junit 3Java:使用 Junit 3 进行异常测试
【发布时间】:2012-10-26 11:55:25
【问题描述】:

我想为IndexOutOfBoundsException 写一个测试。请记住,我们应该使用 JUnit 3。

我的代码:

public boolean ajouter(int indice, T element) {
    if (indice < 0 || indice > (maListe.size() - 1)) {
        throw new IndexOutOfBoundsException();
    } else if (element != null && !maListe.contains(element)) {
        maListe.set(indice, element);
        return true;
    }
}

经过一些研究,我发现您可以使用 @Test(expected = IndexOutOfBoundsException.class) 在 JUnit 4 中执行此操作,但我在哪里找不到在 JUnit 3 中执行此操作的方法。

如何使用 JUnit 3 进行测试?

【问题讨论】:

    标签: java exception junit error-handling junit3


    【解决方案1】:

    在 JUnit 3 中测试异常使用这种模式:

    try {
         ... code that should throw an exception ...
    
         fail( "Missing exception" );
    } catch( IndexOutOfBoundsException e ) {
         assertEquals( "Expected message", e.getMessage() ); // Optionally make sure you get the correct message, too
    }
    

    fail() 确保在代码未引发异常时您会收到错误。

    我在 JUnit 4 中也使用这种模式,因为我通常希望确保正确的值在异常消息中可见,而 @Test 无法做到这一点。

    【讨论】:

    • 在 JUnit 4 中,您可以在 @Test 中设置预期的异常,但您也可以使用更灵活的ExpectedExceptions 并允许检查消息。
    • 啊,不知道这个规则。它是在 JUnit 4.7 中添加的。
    【解决方案2】:

    基本上,如果它没有抛出正确的异常 - 或者如果它抛出其他任何东西,你需要调用你的方法并失败:

    try {
      subject.ajouter(10, "foo");
      fail("Expected exception");
    } catch (IndexOutOfBoundException expect) {
      // We should get here. You may assert things about the exception, if you want.
    }
    

    【讨论】:

      【解决方案3】:

      一个简单的解决方案是在单元测试中添加一个try catch,并在没有抛出异常时让测试失败

      public void testAjouterFail() {
        try {
          ajouter(-1,null);
          JUnit.fail();
        catch (IndexOutOfBoundException()) {
          //success
        }
      }
      

      【讨论】:

        【解决方案4】:

        您可以做的一件事是使用布尔值来运行测试以完成,然后您可以使用断言来验证是否引发了异常:

        boolean passed = false;
        try
        {
            //the line that throws exception
            //i.e. illegal argument exception
            //if user tries to set the property to null:
            myObject.setProperty(null);
        }
        catch (IllegalArgumentException iaex)
        {
            passed = true;
        }
        assertTrue("The blah blah blah exception was not thrown as expected"
                      , passed);
        

        通过使用此测试,您的测试将永远不会失败,并且您可以验证是否引发了特定的异常类型。

        【讨论】:

          【解决方案5】:

          用一些(静态导入)语法糖扩展@Aaron 的解决方案允许编写:

              expected(MyException.class,
                  new Testable() {
                      public void test() {
                      ... do thing that's supposed to throw MyException ...
                      }
                  });
          

          Testable 就像一个 Runnable,它使用 test() 签名抛出 Throwable。

          public class TestHelper {
              public static void expected(Class<? extends Throwable> expectedClass, Testable testable) {
                  try {
                      testable.test();
                      fail("Expected "+ expectedClass.getCanonicalName() +" not thrown.");
                  } catch (Throwable actual) {
                      assertEquals("Expected "+ expectedClass.getCanonicalName() +" to be thrown.", expectedClass, actual.getClass());
                  }
              }
          
              interface Testable {
                  public void test() throws Throwable;
              }
          }
          

          您可以根据需要添加对异常消息的检查。

          【讨论】:

            【解决方案6】:

            在您的测试方法中,在try..catch 块内调用ajouter(),并给出indice 的值,该值应该会引发异常,并带有

            • 一个捕获IndexOutOfBoundsExceptioncatch 子句:在这种情况下,从您的测试方法返回,因此表示通过。
            • 捕获Throwable 的第二个catch 子句:在这种情况下声明失败(调用fail()),因为抛出了错误的异常类型
            • try..catch 声明失败后(调用fail()),因为没有抛出异常。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-01-28
              • 2012-01-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-04
              • 1970-01-01
              相关资源
              最近更新 更多