AssertJ:记录所有通过(和失败)的断言
创始人
2024-09-20 16:00:46
0

要记录所有通过和失败的断言,可以使用AssertJ的条件断言和自定义的监听器。下面是一个示例:

import org.assertj.core.api.Assertions;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AssertionInfo;
import org.assertj.core.api.Condition;
import org.assertj.core.api.WritableAssertionInfo;
import org.assertj.core.internal.Failures;

import java.util.ArrayList;
import java.util.List;

public class AssertJListenerExample {

    private static List passedAssertions = new ArrayList<>();
    private static List failedAssertions = new ArrayList<>();

    public static void main(String[] args) {
        // 注册自定义监听器
        Assertions.setRemoveAssertJRelatedElementsFromStackTrace(false);
        Assertions.setAssertJFailureListener(new CustomAssertJFailureListener());

        // 执行断言
        String name = "John Doe";
        assertThat(name).isEqualTo("John");

        // 输出通过的断言和失败的断言
        System.out.println("Passed assertions: " + passedAssertions);
        System.out.println("Failed assertions: " + failedAssertions);
    }

    public static  CustomStringAssert assertThat(T actual) {
        return new CustomStringAssert(actual);
    }

    public static class CustomStringAssert extends AbstractAssert {

        public CustomStringAssert(T actual) {
            super(actual, CustomStringAssert.class);
        }

        public CustomStringAssert isEqualTo(String expected) {
            isNotNull();

            if (!actual.equals(expected)) {
                String errorMessage = String.format("Expected <%s> but was <%s>", expected, actual);
                throwAssertionError(errorMessage);
            }

            passedAssertions.add("Assertion: " + actual + " is equal to " + expected);
            return this;
        }

        private void throwAssertionError(String errorMessage) {
            AssertionInfo info = new WritableAssertionInfo();
            Failures.instance().failure(info, new Condition<>("", o -> false));
            failedAssertions.add("Assertion: " + actual + " is equal to " + expected);
        }
    }

    public static class CustomAssertJFailureListener extends Failures.AssertionFailureListener {

        @Override
        public void onFailure(AssertionInfo assertionInfo, AssertionError assertionError) {
            if (assertionError instanceof Failures.AssertionErrorWithCondition) {
                String assertion = assertionInfo.descriptionText();
                failedAssertions.add(assertion);
            }
        }
    }
}

在上述示例中,我们定义了一个CustomStringAssert类来执行自定义的字符串断言。在每个断言的方法中,我们检查实际值和期望值是否相等。如果不相等,我们将抛出一个AssertionError,并将其添加到失败的断言列表中。如果相等,我们将通过信息添加到通过的断言列表中。

我们还定义了一个CustomAssertJFailureListener类来捕获AssertJ的断言失败。在这个监听器中,我们检查断言错误是否是由于我们自定义的断言失败引起的,并将相应的断言添加到失败的断言列表中。

最后,在主方法中,我们注册了自定义的监听器,并执行断言。然后,我们可以输出通过的断言和失败的断言列表。

相关内容

热门资讯

pokernow辅助工具!we... pokernow辅助工具!wepoker破解器(透视)app-果然关于是真的挂pokernow辅助工...
佛手在线大菠萝为什么都输!we... 佛手在线大菠萝为什么都输!wepoker怎么开辅助(透视)挂-果然有挂有挂1、任何佛手在线大菠萝为什...
wepoker辅助器激活码!w... wepoker辅助器激活码!wepoker安装教程(透视)攻略-真是揭幕存在有挂1、在wepoker...
wepoker底牌透视!wep... wepoker底牌透视!wepoker有没有挂(透视)app-竟然推荐真的是有挂1)wepoker有...
hhpoker德州透视!pok... hhpoker德州透视!pokemmo辅助工具(透视)方法-果然开挂真的是有挂1、每一步都需要思考,...
wepoker透视app下载!... wepoker透视app下载!werplan怎么透视(透视)方法-果然解迷是真的挂1、wepoker...
hhpoker免费透视脚本!w... hhpoker免费透视脚本!wepoker是不是有人用挂(透视)器-原来有挂是有挂1、wepoker...
wepoker私人定制透视!德... wepoker私人定制透视!德州局透视(透视)神器-原来专业存在有挂1、让任何用户在无需wepoke...
哈糖大菠萝可以开挂吗!德普之星... 哈糖大菠萝可以开挂吗!德普之星私人局辅助免费(透视)插件-好像开挂是有挂1、下载好德普之星私人局辅助...
wepoker私人辅助器!wp... wepoker私人辅助器!wpk私人局辅助是真的吗(透视)神器-一贯有挂有挂wpk私人局辅助是真的吗...