JUnit testing public boolean equals(Object obj)

java unit test for equals
junit test case for boolean methods
how to assert object in junit
junit assert string equals
openpojo test equals and hashcode
lombok equals and hashcode unit test
assert that junit
equalstester

I am working on JUnit testing a project I'm working on, and ran into a wall testing the following method:

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. 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);

The other answer by Nikolas nicely explains why your current tests are pointless. 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).

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.

java - 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: public boolean equals(Object obj) { if (!(obj instanceof  Obviously, when I was calling .equals(Matrix), it was working fine. However, I'm assuming that assertEquals must be passing Object everywhere. My class didn't have a .equals(Object) method and was therefore falling over. I guess all I need to do now is check whether it's an instance of Matrix before casting and it should be fine.

There is no need to test Java's instanceof operator, because it is a built-in functionality of the JVM, not developed by you.

You should rather test your equals method. 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.

SelfEquals Testing an object for equality with itself will , Testing an object for equality with itself will always be true. Severity: ERROR assertThat; import org.junit.Assert; /** * Positive simpleField); } public boolean test3(SelfEqualsPositiveCase obj) { if (obj == null || getClass() != obj.getClass())  Incase of user defined objects, the objects should override equals() and hashCode() methods. Then assertArrayEquals() method called equals and hashCode methods to check the equality on each object.

The Java's instanceof is part of the JVM instruction set, it's a specific same-named instruction. If interested, read the Chapter 6. The Java Virtual Machine Instruction Set: 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.

Note that the method Object::equals is irrelevant to do with this operator/instruction.

Java: "equals(Object obj)" should test argument type, public boolean equals(Object obj) { if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; MyClass mc = (MyClass)obj; // } © 2008  Incase if both expected and actual values are null, then this method returns equal. In the below example, the first Test (mySimpleEqualsTest()) compares two strings. The second test (myObjectEqualsTest()) we are comparing two different user defined objects. The assertEquals() method calls equals method on each object to check equality.

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.

Click reference hamcrest instanceOf()

junit4/SynchronizedRunListenerTest.java at master · junit-team , junit4/src/test/java/org/junit/runner/notification/SynchronizedRunListenerTest.java​. Find file Copy path public boolean equals(Object obj) {. if (this == obj) {. Testing equals, hashCode, et al. in Java; API Change that breaks at Runtime; Complex Data Types in Maven Plugin Configurations; Ad-hoc Configurations; hibernate4-maven-plugin 0.2.0 released! January; 2012

Overriding Object's equals() method, The public boolean equals(Object obj) method comes from the Object class. According to its Test; import junit.framework.Assert; public class  other - the object to compare to. Returns: the result of the comparison. See Also: Comparable.compareTo(T), SortedSet.comparator() equals public boolean equals (java.lang.Object obj) Overrides: equals in class java.lang.Object; hashCode public int hashCode() Overrides: hashCode in class java.lang.Object

Comparing Java objects with equals() and hashcode(), In this Java Challenger you'll learn how equals() and hashcode() combine to make object Duration: 9:34 Posted: Aug 22, 2019 Asserts that expected and actual iterables are deeply equal.. Similarly to the check for deep equality in assertArrayEquals(Object[], Object[], String), if two iterables are encountered (including expected and actual) then their iterators must return equal elements in the same order as each other.

Assertion method Assert.assertEquals() example., The assertEquals() method calls equals method on each object to check equality. @Test. public void myObjectEqualsTest(){. Employee expectedEmpObj  Java JUnit is a unit testing framework for the Java language, and the core content of JUnit is assert. Its main function is to judge whether the result of our program execution is consistent with our expectation. The assertEquals is one of the assertions. The assert methods are provided by… Continue Reading Assertequals Java with JUnit

Comments
  • what's wrong with this? you mean your equals() method is not called ?
  • In JUnit, one shouldn't test java libraries or external libraries, one should test his code. You should test equals function, not instanceof keyword
  • You wrote your answer while I was writing mine offline, but well, our answers are bit different, so I guess I will keep mine around, too ;-)
  • @GhostCat similar answers, but not equal ;-)
  • I like your links here, and I like that you left some "space" for other answers ;-)