2

I have a JAVA class NoName whose objects have the method getProperties(). This method returns an Array of Property. When I now have two instances of NoName, how can I use assertEquals to check whether both instances' Property-Arrays are the same?

_assertEquals(inst.getProperties(), ance.getProrties())_ won't do the job, because it's deprecated.

And since the NoName class is a library class I cannot overwrite equals() (which seems to be the usual solution for this kind of problem, as far as I read until now).

Thanks in advance.

UVM
  • 9,776
  • 6
  • 41
  • 66
keinabel
  • 1,002
  • 3
  • 15
  • 33
  • Possible duplicate of [Comparing arrays in JUnit assertions, concise built-in way?](http://stackoverflow.com/questions/4228161/comparing-arrays-in-junit-assertions-concise-built-in-way) – Joe Oct 23 '15 at 13:41

4 Answers4

4
assertThat(ob1.getProperties(), 
    IsArrayContainingInOrder.contains(obj2.getProperties));

This is using a Hamcrest Matcher which I believe to be a preferable method to doing asserts since the output on failure is much more descriptive.

There is also an IsArrayContainingInAnyOrder if order does not matter.

IsArrayContainingInAnyOrder

John B
  • 32,493
  • 6
  • 77
  • 98
  • Is this a special library I have to include into my workspace? Because then it won't do the job. – keinabel Jun 11 '12 at 12:42
  • It is in the Hamcrest library. A small portion of Hamcrest Matchers comes in the JUnit jar, but the rest are in the Hamcrest-core and Hamcrest-all jars. I recommend that you list these two jars earlier in your class-path than the JUnit jar to ensure you get these Matchers before the default JUnit matchers. – John B Jun 11 '12 at 15:29
1

Since you're talking about comparing an internal structure of an object, you can either override the equals method for the NoName class, and inside that compare the array of properties for both objects. But then, you'll need to take care of the hashCode method too.

Or, you can simply create a helper method hasSameProperties(NoName obj) in the NoName class, and make that method return a boolean flag after comparing the property arrays of both the objects. Then in JUnit, you can simply use the assertTrue method.

anirvan
  • 4,797
  • 4
  • 32
  • 42
0
Property[] one = inst.getProperties(), two = ance.getProperties();
assertEquals(one.length, two.length);
for (int i=0; i<one.length; i++) assertEquals("index #" + i, one[i], two[i]);

That provides a basic scrub. If you're worried about some other edge conditions (e.g., like the array might be null, or that the array is allowed to have a different order as long as all the same elements are in each), then some extra work will be required (e.g., null check, a sort if Property implements Comparable).

Carl
  • 7,538
  • 1
  • 40
  • 64
  • Although I think Hamcrest is a better option here, if this mechanism is used a "reason" should be provided: `assertEquals("index #" + i, one[i], two[i]);` otherwise the provided error message will be useless. – John B Jun 06 '12 at 13:07
  • @John B: excellent point, I will make that adjustment to the answer. re Hamcrest, yes, but: that's a question of engineering that depends on scale - of organization doing the testing, of testing for this code, etc. If this is a quickie (e.g., junit to aid in small scale debugging), it's probably more hassle to sort getting the path to the library correct than any additional info would be worth. – Carl Jun 06 '12 at 14:12
  • Ah, the beauty of Maven. I always just think "Oh, I'll just add that dependency." – John B Jun 06 '12 at 14:39
0

Hamcrest 1.3 uses a slightly different syntax, some examples:

import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;

...

@Test
public void inOrder1() {
    assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")));
}

@Test(expected =  AssertionError.class)
public void inOrder2() {
    assertThat(new String[]{"bar", "foo"}, arrayContaining(equalTo("foo"), equalTo("bar")));
    // Expected: ["foo", "bar"]
    //      but: item 0: was "bar"
}

@Test(expected =  AssertionError.class)
public void inOrder3() {
    assertThat(new String[]{"foo", "bar", "lux"}, arrayContaining(equalTo("foo"), equalTo("bar")));
    // Expected: ["foo", "bar"] in any order
    //      but: Not matched: "lux"
}

@Test(expected =  AssertionError.class)
public void inOrder4() {
    assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar"), equalTo("lux")));
    // Expected: ["foo", "bar", "lux"] in any order
    //      but: No item matched: "lux" in ["foo", "bar"]
}
Oscar Scholten
  • 570
  • 5
  • 13