简体   繁体   English

使用相同的异常测试多行代码

[英]Test multiple lines of codes w.r.t same Exception

I have a code segment like shown below. 我有如下所示的代码段。 Each line of code throw same exception. 每行代码都引发相同的异常。 However, in practice, when first line throws an exception, testFoo finishes its job and does not continue, as expected. 但是,实际上,当第一行引发异常时,testFoo将完成其工作,并且不会按预期继续。 But, I want a bit more different thing; 但是,我想要更多不同的东西; since they are throwing same exception, I want to continue and check these three lines wrt the exception which they all throw. 由于它们都抛出相同的异常,因此我想继续检查这三行是否都抛出了异常。 If they throw, test should be continue. 如果他们扔了,应该继续测试。

How can I test these three line wrt same exception? 如何测试这三行wrt相同的异常?

  @test
   void testFoo(){

       assertNull( /*errorMessage*/, ClassFoo.foo(null));    // foo will throw `AssertionError` due to null parameter
       assertNull( /*errorMessage*/, ClassBar.bar(null));    // foo will throw `AssertionError` due to null parameter
       assertNull( /*errorMessage*/, ClassGbr.gbr(null));   // foo will throw `AssertionError` due to null parameter
    }

I have tried to implement precondition for each parameters to a method with Java built-in Assert.assertTrue... 我试图使用Java内置的Assert.assertTrue为方法的每个参数实现前提条件...

This is not built into java, this is from junit: void junit.framework.Assert.assertTrue(...) . 这不是Java内置的,它来自junit: void junit.framework.Assert.assertTrue(...) You are confusing Java Assertions with Junit assertions. 您正在将Java断言与Junit断言混淆。

Junit assertions should be used in your unit tests. Junit断言应在单元测试中使用。 They look like Assert.assertEquals(result, "expected result"); 它们看起来像Assert.assertEquals(result, "expected result"); They are intended to test the validity of the methods under test in your unit tests. 它们旨在测试单元测试中被测方法的有效性。

Java assertions should be used when verifying assumptions. 验证假设时,应使用Java断言。 They look like assert param!=null:"param should not be null!"; 它们看起来像assert param!=null:"param should not be null!"; They are part of the java language and can be turned on and off at compile time. 它们是Java语言的一部分,可以在编译时打开和关闭。 They are intended to double check assumptions in your code and to produce zero overhead when turned off. 它们旨在仔细检查代码中的假设,并在关闭时产生零开销。

Programming with assertions is a great thing. 用断言进行编程是一件好事。 Using Junit assertions outside of unit tests is dubious. 在单元测试之外使用Junit断言是可疑的。

My interpretation of this question is that you are expecting an AssertFailedError in your unit test and this is meant to be part of this test. 我对这个问题的解释是,您在单元测试中期望有一个AssertFailedError,这将成为该测试的一部分。 If that is the case, you can use the following junit method structure: 如果是这种情况,可以使用以下junit方法结构:

@Test(expected = AssertFailedError.class)
public void testFoo() throws AssertFailedError
{
  assertNotNull(null);
}

You can use this when you are testing a block of code that you know will throw an exception. 当您测试知道会引发异常的代码块时,可以使用此功能。

Just catch the exception yourself: 自己捕获异常:

@Test
void testFoo() {
    boolean fooHasThrownException = false;
    boolean barHasThrownException = false;
    boolean gbrHasThrownException = false;

    try {
        ClassFoo.foo(null);
        fail();
    } catch (AssertionError e) {
        fooHasThrownException = true;
    }

    try {
        ClassBar.bar(null);
        fail();
    } catch (AssertionError e) {
        barHasThrownException = true;
    }

    try {
        ClassGbr.gbr(null);
        fail();
    } catch (AssertionError e) {
        gbrHasThrownException = true;
    }

    assertThat(true, equalTo(fooHasThrownException),
                     equalTo(barHasThrownException),
                     equalTo(gbrHasThrownException));
}

Note that your assertNull() is redundant. 请注意,您的assertNull()是多余的。 If a method throws an exception, it will not return anything. 如果方法抛出异常,它将不会返回任何内容。

On the side, this is a very weird scenario to be testing. 从侧面看,这是一个非常奇怪的测试场景。 If a method throws an exception, it just seems more logical to stop any further processing, if those processes down the line are going to also throw exceptions anyway. 如果一个方法抛出异常,那么停止任何进一步的处理似乎更加合乎逻辑,如果这些流程的末尾的这些进程仍然会抛出异常。

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

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