简体   繁体   English

JUnit有什么更好的方法在两个对象之间使用assertEquals而不定义equals方法

[英]JUnit what better way to use assertEquals between two objects without defining equals method

I want to know what could be the better way to use 我想知道什么是更好的使用方式

assertEquals() assertEquals()

between two objects without defining 在两个对象之间没有定义

equals() 等于()

method. 方法。

I wanted to use the org.apache.commons.lang3.builder.EqualsBuilder : Assert.assertTrue(EqualsBuilder.reflectionEquals(expected,actual)); 我想使用org.apache.commons.lang3.builder.EqualsBuilder:Assert.assertTrue(EqualsBuilder.reflectionEquals(expected,actual));

But i read in the documentation that it is not the best way to compare two objects : COMMONS Doc . 但是我在文档中读到,这不是比较两个对象的最佳方法: COMMONS Doc

Also I would like to find an open source library that can do that or even better : no library. 我也想找到一个可以做到甚至更好的开源库:没有库。

Thank you. 谢谢。

我认为有关SecurityManager问题的建议不适用于您的测试代码,因为您可以控制运行测试的环境。

I believe you can use something like: 我相信您可以使用类似:

assertThat(Object1, is(Object2));

Using Hamcrest . 使用Hamcrest

如果可以使用Lombok项目,则可以自动生成等于项: https : //projectlombok.org/features/EqualsAndHashCode.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在jUnit中计算assertEquals()方法中expected_value的最佳方法是什么 - what is the best way to calculate the expected_value in assertEquals() method in jUnit Equals使用String对象进行比较的assertEquals方法 - Equals Method for assertEquals with String-objects to compare Junit 4 assertEquals(...)无法比较两个对象 - Junit 4 assertEquals(…) fails to compare two objects 比较两个对象时,JUnit assertEquals()不起作用 - JUnit assertEquals() not working when comparing two objects 如何使用assertEquals或JUNIT测试中的任何其他方式比较两个对象? - How to compare two objects using assertEquals or by any other way in JUNIT testing? 为了在测试报告中看到比较时对象的哪些字段不同,JUnit的assertEquals()方法的替代方法是什么? - What are alternatives to JUnit's assertEquals() method in order to see in the test report which fields of objects differed on comparison? Java - 在 JUnit 测试中不实施 equals 方法的对象的深度比较 - Java - Deep comparison of objects without implementing the equals method in JUnit Tests 在Java中使用.equals方法的正确方法是什么? - What is the proper way to use a .equals method in Java? 比较两个对象的类的更好方法是什么 - What is better way to compare class of two objects 在两个对象上的assertEquals() - assertEquals() on two objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM