拦截 JUnit Assert 函数

发布于 2024-10-26 04:05:50 字数 476 浏览 2 评论 0原文

当 JUnit 中的断言失败时,我想做一些“自己的事情”。我想要这样:

public class MyAssert extends org.junit.Assert {

    // @Override
    static public void fail(String message) {
        System.err.println("I am intercepting here!");
        org.junit.Assert.fail(message);
    }
}

当然,这是行不通的,因为你不能重写静态方法。但如果可以的话,那就太好了,因为像 assertTrue() 这样的每个断言函数都会调用 fail() 方法。因此,我可以轻松拦截每一个断言。

有没有什么方法可以做我想做的事情,而不需要实现所有不同风格的 assert...

I would like to do some "own stuff" when an assertion in JUnit fails. I would like to have this:

public class MyAssert extends org.junit.Assert {

    // @Override
    static public void fail(String message) {
        System.err.println("I am intercepting here!");
        org.junit.Assert.fail(message);
    }
}

Of course, this does not work, because you cannot override static methods. But if it would, this would be nice, because every assert function like assertTrue() calls the fail() method. Thus, I could easily intercept every assertion.

Does there exist any way to do what I want to do here, without implementing all different flavors of assert...?

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

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

发布评论

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

评论(4

生活了然无味 2024-11-02 04:05:50

您应该查看 Junit 4.7 中引入的规则。特别是 TestWatchman:

TestWatchman 是记录测试操作而不对其进行修改的规则的基类。例如,此类将保留每个通过和失败测试的日志:

http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatchman.html

它允许您定义一个可以处理失败测试的 MethodRule (复制自javadoc):

   @Rule
    public MethodRule watchman= new TestWatchman() {
            @Override
            public void failed(Throwable e, FrameworkMethod method) {
                    watchedLog+= method.getName() + " " + e.getClass().getSimpleName()
                                    + "\n";
            }

            @Override
            public void succeeded(FrameworkMethod method) {
                    watchedLog+= method.getName() + " " + "success!\n";
            }
    };

    @Test
    public void fails() {
            fail();
    }

    @Test
    public void succeeds() {
    }

根据评论,TestWatchman 在较新版本的 Junit 中已被废弃,并被 TestWatcher 取代(但功能相同):

http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatcher.html

You should take a look at the Rules, that were introduced in Junit 4.7. Especially TestWatchman:

TestWatchman is a base class for Rules that take note of the testing action, without modifying it. For example, this class will keep a log of each passing and failing test:

http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatchman.html

It lets you define a MethodRule that can handle failing Tests (Copy from the javadoc):

   @Rule
    public MethodRule watchman= new TestWatchman() {
            @Override
            public void failed(Throwable e, FrameworkMethod method) {
                    watchedLog+= method.getName() + " " + e.getClass().getSimpleName()
                                    + "\n";
            }

            @Override
            public void succeeded(FrameworkMethod method) {
                    watchedLog+= method.getName() + " " + "success!\n";
            }
    };

    @Test
    public void fails() {
            fail();
    }

    @Test
    public void succeeds() {
    }

As per comment TestWatchman is depecreated in newer Versions of Junit and replaced with TestWatcher (but the functionality is the same):

http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatcher.html

匿名。 2024-11-02 04:05:50

您可以编写一个类,该类实现 Assert 中所有方法的完全相同的签名,然后委托给 Assert 方法。

public class MyAssert {
    static public void assertTrue(String message, boolean condition) {
        Assert.assertTrue(message, condition);
    }

    ...

    static public void fail(String message) {
        System.err.println("I am intercepting here!");
        Assert.fail(message);
    }
}

并不完全重新实现所有方法,但仍然很乏味。您的 IDE 可能能够帮助生成委托方法。

You could write a class that implements exactly the same signatures of all the methods in Assert and then delegates to the Assert methods.

public class MyAssert {
    static public void assertTrue(String message, boolean condition) {
        Assert.assertTrue(message, condition);
    }

    ...

    static public void fail(String message) {
        System.err.println("I am intercepting here!");
        Assert.fail(message);
    }
}

Not exactly re-implementing all the methods, but tedious nonetheless. Your IDE may be able to help in generating the delegate methods.

濫情▎り 2024-11-02 04:05:50

对于单独的断言,您可以捕获 AssertionError - 以下内容对我有用。当您只是偶尔需要该功能时,它很有用。

try {
    Assert.assertEquals("size", classes.length, containerList.size());
} catch (AssertionError e) {
    System.err.println("ERROR");
    for (AbstractContainer container : containerList) {
        System.err.println(container.getClass());
    }
    throw (new RuntimeException("Failed", e));
}

For indivdual assertions you can catch the AssertionError - the following works for me. It's useful when you only need the functionality occasionally.

try {
    Assert.assertEquals("size", classes.length, containerList.size());
} catch (AssertionError e) {
    System.err.println("ERROR");
    for (AbstractContainer container : containerList) {
        System.err.println(container.getClass());
    }
    throw (new RuntimeException("Failed", e));
}
与风相奔跑 2024-11-02 04:05:50

TestWatchman 被 TestWatcher 替换,它在测试方法级别而不是断言级别进行拦截,并且 Assert 类静态方法不能被重写,因此另一种方法是创建自己的断言方法来断言对象,该方法应该适用于大多数断言

TestWatchman is replaced with TestWatcher and it intercepts at test method level not Assert level and the Assert class static methods cannot be overridden so an alternative is to create your own assert method to assert objects which should work for most of the asserts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文