Assert4j:如何根据实际值忽略字段
创始人
2024-09-20 15:01:37
0

在使用AssertJ进行断言时,如果需要根据实际值忽略某些字段,可以使用自定义比较器或者自定义条件来实现。以下是两种解决方法的代码示例:

  1. 使用自定义比较器:
import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.Condition;
import org.assertj.core.api.ObjectAssert;
import org.assertj.core.internal.Iterables;

import java.util.Objects;

public class CustomComparatorExample {

    public static class Person {
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        // Getters and Setters
    }

    public static void main(String[] args) {
        Person actual = new Person("John", 30);
        Person expected = new Person("John", 25);

        ObjectAssert personAssert = assertThat(actual, usingComparatorForFields((field, actualValue, expectedValue) -> {
            if ("age".equals(field.getName())) {
                // 忽略age字段的比较
                return true;
            }
            // 使用默认的比较器比较其他字段
            return Objects.equals(actualValue, expectedValue);
        }, "name"));

        personAssert.isEqualTo(expected);
    }

    public static  ObjectAssert assertThat(T actual, Condition condition, String... propertiesOrFieldsUsedInMessage) {
        return new ObjectAssert<>(actual, condition, propertiesOrFieldsUsedInMessage);
    }

    public static  ObjectAssert assertThat(T actual, Condition condition) {
        return new ObjectAssert<>(actual, condition);
    }

    public static  ObjectAssert assertThat(T actual) {
        return new ObjectAssert<>(actual);
    }

    public static  AbstractObjectAssert assertThat(T actual, Class clazz) {
        return new ObjectAssert<>(actual, clazz);
    }

    public static  AbstractObjectAssert assertThat(T actual, Class clazz, String... propertiesOrFieldsUsedInMessage) {
        return new ObjectAssert<>(actual, clazz, propertiesOrFieldsUsedInMessage);
    }

    public static  AbstractObjectAssert assertThat(T actual, String... propertiesOrFieldsUsedInMessage) {
        return new ObjectAssert<>(actual, propertiesOrFieldsUsedInMessage);
    }

    public static  AbstractObjectAssert assertThat(T actual, String description, String... propertiesOrFieldsUsedInMessage) {
        return new ObjectAssert<>(actual, description, propertiesOrFieldsUsedInMessage);
    }

    public static  Condition usingComparatorForFields(Iterables.GetByFieldOrPropertyName getter,
                                                            Comparator comparator,
                                                            String... fields) {
        return new Condition<>(new FieldOrPropertyComparator<>(getter, comparator, fields));
    }

    public static  Condition usingComparatorForFields(Condition condition, String... fields) {
        return new Condition<>(new FieldOrPropertyComparator<>(Iterables::getByFieldOrPropertyName, condition, fields));
    }
}
  1. 使用自定义条件:
import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.Condition;
import org.assertj.core.api.ObjectAssert;
import org.assertj.core.internal.Iterables;

import java.util.Objects;

public class CustomConditionExample {

    public static class Person {
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        // Getters and Setters
    }

    public static void main(String[] args) {
        Person actual = new Person("John", 30);
        Person expected = new Person("John", 25);

        ObjectAssert personAssert = assertThat(actual, ignoringFields("age"));

        personAssert.isEqualTo(expected);
    }

    public static  ObjectAssert assertThat(T actual, Condition condition, String... propertiesOrFieldsUsedInMessage) {
        return new ObjectAssert<>(actual, condition, propertiesOrFieldsUsedInMessage);
    }

    public static  ObjectAssert assertThat(T actual, Condition condition) {
        return new ObjectAssert<>(actual, condition);
    }

    public static  ObjectAssert assertThat(T actual) {
        return new ObjectAssert<>(actual);
    }

    public static  AbstractObjectAssert assertThat(T actual, Class clazz) {
        return new ObjectAssert<>(actual, clazz);
    }

    public static  Abstract

相关内容

热门资讯

wepoke透视挂靠谱吗,wp... 您好,wepoke透视挂靠谱吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
微扑克的辅助工具苹果,wepo... 微扑克的辅助工具苹果,wepoke软件太假了,智星德州菠萝辅助工具1、不需要AI权限,帮助你快速的进...
智星德州菠萝辅助,wepoke... 自定义wepokeai辅助系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,...
德州之星辅助挂,wepokea... 1、德州之星辅助挂,wepokeai代打,智星德州菠萝辅助;代表性(透视辅助软件透明挂)高科技在四个...
微扑克有机器人吗,wepoke... 自定义wepoke黑科技靠谱吗系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助...
wpk透视辅助合作,wpk辅助... wpk透视辅助合作,wpk辅助器小程序,智星德州辅助器1、金币登录送、破产送、升级送、活动送。详细领...
aapoker有外挂吗,wep... 您好,wepoke到底有没有挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
德扑ai工具,wepokeai... 德扑ai工具,wepokeai代打,智星德州菠萝辅助器推荐1、许多玩家不知道wepokeai代打辅助...
AApoker靠谱吗,wpk透... 一、wpk透明挂简介了解软件请加微:136704302wpk透明挂是一款在线扑克游戏平台,玩家可以在...
wepoke是不是有挂,wpk... wepoke是不是有挂,wpk有透视辅助吗,智星菠萝德州有挂吗1、在wpk有透视辅助吗ai机器人技巧...