将2个数组与其中的kafka生产者记录进行比较时,我的Junit测试失败。失败是在阵列后增加了一个空格。
预期:
java.util.ArrayList<[ProducerRecord(topic=producer-test, partition=null,
headers=RecordHeaders(headers = [], isReadOnly = false), key=0,
value=Message: test-message,
Number: 0, timestamp=null), ProducerRecord(topic=producer-test,
partition=null, headers=RecordHeaders(headers = [], isReadOnly = false),
key=1, value=Message: test-message,
Number: 1, timestamp=null)]>
实际:
java.util.ArrayList<[ProducerRecord(topic=producer-test, partition=null,
headers=RecordHeaders(headers = [], isReadOnly = false), key=0,
value=Message: test-message,
Number: 0, timestamp=null), ProducerRecord(topic=producer-test,
partition=null, headers=RecordHeaders(headers = [], isReadOnly = false),
key=1, value=Message: test-message,
Number: 1, timestamp=null)]>
IDE告诉我,唯一的区别是Expected中arrayList末尾有一个空格,您可以通过突出显示末尾看到。这是怎么回事?!
编辑:
这是其他一些代码
List<ProducerRecord<Integer, TestObj>> history = producer.history();
//To be inserted into expected
TestObj obj0 = new TestObj("test-message", 0);
TestObj obj1 = new TestObj("test-message", 1);
//new arraylist is needed or else the lists have slightly different types for some reason
List<ProducerRecord<Integer, TestObj>> expected = new ArrayList<ProducerRecord<Integer, TestObj>>(Arrays.asList(
new ProducerRecord<Integer, TestObj>("producer-test", 0, obj0),
new ProducerRecord<Integer, TestObj>("producer-test", 1, obj1)
));
Assert.assertEquals("Sent didn't match expected!", expected, history);
答案 0 :(得分:2)
addMention(user: any): void {
const textarea = document.querySelector('textarea');
const mentionSelect = <HTMLElement>document.querySelector('.mention-container');
const value = textarea.value; // Store the value
textarea.value = `${textarea.value}${user.value}`; // Current Value
mentionSelect.style.display = 'none'; // Hide the mention list
this.message = textarea.value; // Make sure the new textarea value is stored in message
textarea.focus(); // refocus
textarea.value = ''; // clear
textarea.value = value; // add the value back in
}
正在调用Assert.assertEquals()
expected.equals(history)
的定义如下:
将指定的对象与此列表进行比较以确保相等。当且仅当指定对象也是一个列表,并且两个列表具有相同的大小,并且两个列表中所有对应的元素对相等时,才返回true。 (如果(e1 == null?e2 == null:e1.equals(e2)),则两个元素e1和e2相等。)换句话说,如果两个列表包含相同顺序的相同元素,则两个列表定义为相等。 。此定义可确保equals方法可在List接口的不同实现中正常工作。
您是否为ProducerRecord类定义了List.equals(Object o)
方法?
如果没有,则equals
对象将通过引用相等进行比较,这意味着仅在它们实际上是相同的对象的基础上才进行比较。由于您是在测试中构造新的ProducerRecord
对象,因此它们将不具有引用相等性。
答案 1 :(得分:0)
要比较两个对象或两个对象列表,可以重写(等于)该对象的方法并在那里比较该对象的所有元素。如果它们都匹配,则可以返回true等于。检查以下示例:
public class Customer {
String name;
int age;
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
// it checks if the argument is of the type Customer by comparing the classes
// of the passed argument and this object.
// if(!(obj instanceof Customer)) return false; ---> avoid.
if(obj == null || obj.getClass()!= this.getClass())
return false;
// type casting of the argument.
Customer customer = (Customer) obj;
if(customer.getName().equals(this.name) &&
(customer.getAge() == this.age))
return true;
return false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}