简体   繁体   English

JUnit测试公共布尔等于(对象obj)

[英]JUnit testing public boolean equals(Object obj)

I am working on JUnit testing a project I'm working on, and ran into a wall testing the following method: 我正在JUnit测试我正在处理的项目,并且遇到了墙上测试以下方法的问题:

public boolean equals(Object obj) {
  if (!(obj instanceof Vehicle)) {
     return false;
  }
  else {
     Vehicle other = (Vehicle) obj;
     return (owner + yearMakeModel + value).
        equals(other.owner + other.yearMakeModel
        + other.value);

I am using the following tests to test instnaceof, however, it does not seem to be testing the correct code. 我正在使用以下测试来测试instnaceof,但是,它似乎并未测试正确的代码。 After submitting, This section of my code is highlighted and I am seeing a message that this method is not executed in my testing. 提交后,代码的这一部分将突出显示,并且我看到一条消息,表明我的测试未执行此方法。

/** 
 * instanceof test false.
 */ 
 @Test public void instanceOfFalseTest() {
      Car car1 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
      Object obj = new Object();
      Assert.assertFalse(obj instanceof Vehicle);
   } 
/** 
 * instanceof test true.
 */ 
@Test public void instanceOfTrueTest() {
      Car car1 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
      Object obj = new Object();
      Assert.assertTrue(car1 instanceof(Vehicle);

There is no need to test Java's instanceof operator, because it is a built-in functionality of the JVM, not developed by you. 无需测试Java的instanceof运算符,因为它是JVM的内置功能,不是您自己开发的。

You should rather test your equals method. 您应该测试equals方法。 For example like this: 例如这样:

@Test public void equalsFalseTest1() {
    Car car1 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
    Object obj = new Object();
    Assert.assertNotEquals(car1, obj);
}

@Test public void equalsFalseTest2() {
    Car car1 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
    Car car2 = new Car("Doe, John", "2017 Honda Accord", 222000, true);
    Assert.assertNotEquals(car1, car2);
}

@Test public void equalsTrueTest() {
    Car car1 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
    Car car2 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
    Assert.assertEquals(car1, car2);
}

Then your equals method is actually called. 然后实际上调用了equals方法。

The other answer by Nikolas nicely explains why your current tests are pointless. Nikolas的另一个答案很好地解释了为什么当前的测试毫无意义。 A more reasonable test could look like this: 一个更合理的测试如下所示:

Car car1 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
Car car2 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
Car car3 = new Car("Jones, Samual", "2017 Honda Accord", 222000, true);

assertThat(car1, is(car2));
assertThat(car1, not(car3));

where is() refers to a Java Hamcrest matcher (they allow you to write down your test cases in very elegant ways). is()指向Java Hamcrest匹配器(它们允许您以非常优雅的方式写下测试用例)。

You want to think up all the possible ways how two objects could be equal, nor not equal. 您想考虑所有可能的方式,如何使两个对象相等或相等。 And then, you (at least) one test case for each of those cases. 然后,您(至少)对于每个用例都需要一个测试用例。 Ideally, they each go into a separate, independent @Test method. 理想情况下,它们各自进入单独的独立@Test方法。

The Java's instanceof is part of the JVM instruction set, it's a specific same-named instruction. Java的instanceof是JVM指令集的一部分,它是一个特定的同名指令。 If interested, read the Chapter 6. The Java Virtual Machine Instruction Set: 6.5. 如果有兴趣,请阅读第6章。Java虚拟机指令集: 6.5。 Instructions . 说明

Luckily, there is neither a way to @Override the implementation nor change the behavior - this implies this feature is untestable and there is no need to test if an object is an instance of another since the definition using extends and implements key-words. 幸运的是,既没有方法来@Override实现也不能改变行为-这意味着此功能不可测试,并且由于使用定义定义了extendsimplements关键字,因此无需测试对象是否是另一个对象的实例。

Note that the method Object::equals is irrelevant to do with this operator/instruction. 注意,方法Object::equals与该运算符/指令无关。

The other answer: 另一个答案:

 @Test public void instanceOfFalseTest() {
  Object obj = new Object();
  assertThat(obj, instanceOf(Vehicle.class));
} 

 @Test public void instanceOfTrueTest() {
  Car car1 = new Car("Jones, Sam", "2017 Honda Accord", 222000, true);
  Object obj = new Object();
  assertThat(car1, instanceOf(Vehicle.class));
}

, where instanceOf() check if an Object is an instance of a given class. ,其中instanceOf()检查Object是否是给定类的实例。

Click reference hamcrest instanceOf() 单击参考hamcrest instanceOf()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM