JUnit/TestNg:如何简化/组合多个 { try failure() catch } 来处理同一异常?

发布于 2024-11-29 02:39:05 字数 394 浏览 0 评论 0原文

示例(<预期异常> 对于 assert 1assert 2 是相同的):

@junit.framework.Test // or @org.testng.annotations.Test
public void testCase() {
    try {
        // assert 1
        fail();
    } catch (<Expected Exception>) {

    }

    try {
        // assert 2
        fail();
    } catch (<Expected Exception>) {

    }
}

Example (<Expected Exception> for assert 1 & assert 2 is same) :

@junit.framework.Test // or @org.testng.annotations.Test
public void testCase() {
    try {
        // assert 1
        fail();
    } catch (<Expected Exception>) {

    }

    try {
        // assert 2
        fail();
    } catch (<Expected Exception>) {

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

中二柚 2024-12-06 02:39:05

如果你喜欢冒险,你也可以尝试assertThrows:

https://github.com/dsaff/junit .contrib

如果您有任何问题,请随时询问。

If you're feeling adventurous, you can also try out assertThrows:

https://github.com/dsaff/junit.contrib

Feel free to ask if you have any problems.

岁吢 2024-12-06 02:39:05

如果将其分解为单独的测试方法太困难,那么这就是过去对我有用的方法。

创建一个需要 Callback 的方法 expectsException()

interface Callback {
    void call() throws Exception;
}

void expectsException(Callback callback) {
    try {
        callback.call();
        fail("ExpectedException was not thrown!");
    } catch (Exception e) {
        if (!(e instanceof ExpectedException)) {
            fail("Expecting ExpectedException, got " + e.getClass());
        }
        assertEquals("Expected exception message", e.getMessage());
    }
}

然后,将代码包装在 Callback 中的 try {...} catch 块中:

@Test
public void testSomething() {
    expectsException(new Callback() {
        public void call() throws Exception {
            // assert 1
        }
    });

    expectsException(new Callback() {
        public void call() throws Exception {
            // assert 2
        }
    });
}

但是请注意,这取决于您在 中执行的操作catch 块,这可能会或可能不会比简单的 try {...} catch 更详细。

(当 Java 获得适当的闭包时,这种方法将变得更有意义。)

If it's too hard to break this up into individual test methods, here's what's worked for me in the past.

Create a method expectsException() that expects a Callback.

interface Callback {
    void call() throws Exception;
}

void expectsException(Callback callback) {
    try {
        callback.call();
        fail("ExpectedException was not thrown!");
    } catch (Exception e) {
        if (!(e instanceof ExpectedException)) {
            fail("Expecting ExpectedException, got " + e.getClass());
        }
        assertEquals("Expected exception message", e.getMessage());
    }
}

Then, wrap up the code inside your try {...} catch blocks in the Callback:

@Test
public void testSomething() {
    expectsException(new Callback() {
        public void call() throws Exception {
            // assert 1
        }
    });

    expectsException(new Callback() {
        public void call() throws Exception {
            // assert 2
        }
    });
}

Note however, that depending on what you're doing in the catch block, this may or may not end up less verbose than a straightforward try {...} catch.

(When Java gets proper closures, then this approach will make a lot more sense.)

随心而道 2024-12-06 02:39:05

您可能应该将您的方法分成两个单独的方法,每个方法都会抛出:

@Test(expectedExceptions = NullPointerException.class)
public void testCase1() {
  // assert 1
}


@Test(expectedExceptions = NullPointerException.class)
public void testCase2() {
  // assert 2
}

You should probably break your method into two separate methods that will each throw:

@Test(expectedExceptions = NullPointerException.class)
public void testCase1() {
  // assert 1
}


@Test(expectedExceptions = NullPointerException.class)
public void testCase2() {
  // assert 2
}
撩起发的微风 2024-12-06 02:39:05

catch-exception 可能会有所帮助:

public void testCase() {
    // assert 1
    verifyException(obj, MyException.class).do(1);
    // assert 2
    verifyException(obj, MyException.class).do(2);
}

catch-exception might help:

public void testCase() {
    // assert 1
    verifyException(obj, MyException.class).do(1);
    // assert 2
    verifyException(obj, MyException.class).do(2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文