2

I was testing some of my methods for my class and I got the following error:

java.lang.AssertionError: expected: com.company.ArrayList<[ 11 12 ]> but was: com.company.ArrayList<[ 11 12 ]>

I fail to see the difference. Basically what I have is this test:

 public void setUp() throws Exception {
    List ot = new ArrayList();
    ot.add(11);
    ot.add(12);
    prg = new ProgState(ot);

}

prg was declared before in the class, as private.

@Test
public void testGetOut() throws Exception {
    List wtf = new ArrayList();
    wtf.add(11);
    wtf.add(12);
    assertEquals(wtf, prg.getOut());
}

ArrayList is my own ADT, List is interface, ProgState is just:

public class ProgState {
private List out;

public ProgState(List ot) {
    out = ot;
}

public List getOut() {
    return out;
}

It's just returning my list. Why won't it accept ot = [11, 12] being the same as wtf = [11, 12]? I have no idea at all.

Charlotte45
  • 143
  • 1
  • 16
  • 4
    _"ArrayList is my own ADT"_ Then show its equals implementation. – Alexis C. Oct 13 '15 at 21:31
  • 4
    Seems like you implemented your own `com.company.ArrayList`. Can you share its implementation? Specifically - did you overwrite the `equals(Object)` method? – Mureinik Oct 13 '15 at 21:32
  • Yea I did. I ended up not overriding the equals but changing the whole perspective. I made another Array, added the same ints I added in the first one and just used the method .get(0) on both of of them twice and compared the results, changing a flag if I got a wrong answer. I applied the assert on the flag. Thank you for your answer and sorry for not replying back. – Charlotte45 Oct 17 '15 at 20:59

2 Answers2

1

In Java, the .equals() function for Object enforces equality through ==. Because your two lists don't represent the same Object, this check fails during your assertEquals() call.

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
1

ArrayList is my own ADT

I will venture to guess that you haven't overridden the equals method in your ADT. By the default implementation it inherits from Object, all distinct instances (created by new) are NOT equal.

You need to override this method and implement comparing the elements: the sizes must match and the elements must be equal pairwise: both null or both non-null and equals by their equals methods.

janos
  • 120,954
  • 29
  • 226
  • 236