package com.lijun.unitils; import java.util.Calendar; import org.junit.Test; import static org.unitils.reflectionassert.ReflectionAssert.*; import static org.unitils.reflectionassert.ReflectionComparatorMode.*; /** * Unitils断言 * 比较对象 * 1. assertReflectionEquals(期望值,实际值,比较级别):反射断言,为我们提供了各种级别的比较断言 * ( * ReflectionComparatorMode.LENIENT_ORDER:忽略要断言集合collection或者array中元素的顺序 * ReflectionComparatorMode.IGNORE_DEFAULTS:忽略默认值,如引用类型为null,整型类型为0,断言忽略这些值的比较 * ReflectionComparatorMode.LENIENT_DATES:比较两个实例的Date是不是都被设置了值或者为null,而忽略Date的值是否相等 * ) * 2. assertLenientEquals:断言,既忽略顺序又忽略默认值的断言 * 比较属性 * 1. assertPorpertyReflectionEquals * 2. assertPorpertyLenientEquals * */ public class AssertReflectionEqualsTest { @Test public void testReflection() { User user1 = new User("tom","1234"); User user2 = new User("tom","1234"); assertReflectionEquals(user1, user2); } Integer[] orderList1 = new Integer[]{1,2,3}; Integer[] orderList2 = new Integer[]{3,2,1}; @Test public void testLenient() { //测试两个数组的值是否相等,忽略顺序 assertReflectionEquals(orderList1, orderList2, LENIENT_ORDER); assertLenientEquals(orderList1, orderList2); //测试两个对象的值是否相等,忽略时间值是否相等 User user1 = new User("tom","1234"); Calendar cal1 = Calendar.getInstance(); user1.setLastVisit(cal1.getTime()); User user2 = new User("tom","1234"); Calendar cal2 = Calendar.getInstance(); cal2.set(Calendar.DATE, 15); user2.setLastVisit(cal2.getTime()); assertReflectionEquals(user1, user2, LENIENT_DATES); //assertLenientEquals(user1, user2); } public void testProperty() { User user = new User("tom","1234"); assertPropertyReflectionEquals("userName", "tom", user); assertPropertyLenientEquals("lastVist", null, user); } }