Equals Mock Objekte

jCoder1984

Aktives Mitglied
Hallo ich habe eine ganz einfache Frage :

Ich habe ein Controller, der Objekte in eine Liste einfügen soll, sofern sie noch nicht vorhanden sind. Hier der Quellcode :
Java:
    @Override
    public Association addElement(@NonNull Association element) {
        Association tmpElement = null;

        Optional<Association> optional = elementList.stream().filter(e -> e.equals(element)).findFirst();
        if (optional.isPresent()) {
            log.debug("the association '{}' exists already.", element);
            tmpElement = optional.get();
        } else {
            log.debug("add the association '{}'.", element);

            tmpElement = element;
            elementList.add(tmpElement);

            }

            log.debug("notify observer about the new association '{}'", tmpElement);
            setChanged();
            notifyObservers(tmpElement);
        }

        return tmpElement;
    }

Hier nun mein Test :
Java:
    // test parameter
    private final Association association1 = Mockito.mock(Association.class);
    private final Association association2 = Mockito.mock(Association.class);
    private final Association association3 = Mockito.mock(Association.class);

    @Test
    public void testAddElement() {
        Mockito.when(association1.getName()).thenReturn("association1");
        Mockito.when(association2.getName()).thenReturn("association1");
        Mockito.when(association3.getName()).thenReturn("association3");

        Controller<Association> controller = new AssociationController();

        controller.addElement(association1);
        controller.addElement(association2);
        controller.addElement(association3);

        Assert.assertNotNull(controller.getElementList());

        Assert.assertThat("elementList content", Lists.newArrayList(association1, association3),                IsIterableContainingInAnyOrder.containsInAnyOrder(controller.getElementList().toArray()));
    }

Leider schlägt der Test fehlt :
java.lang.AssertionError: elementList content
Expected: iterable over [<Mock for Association, hashCode: 1924582348>, <Mock for Association, hashCode: 2097514481>, <Mock for Association, hashCode: 1568059495>] in any order
but: No item matches: <Mock for Association, hashCode: 2097514481> in [<Mock for Association, hashCode: 1924582348>, <Mock for Association, hashCode: 1568059495>]
Kann mir jemand auf die Sprünge helfen
 
Danke für die Antwort. Nun habe ich das Problem verstanden. Gibte s evtl. eine andere Bibliothek oder besser gesagt eine lösungsidee für das Problem
 
Du kannst dir doch wahrscheinlich einfach so drei Association Instanzen erzeugen (also quasi per 'new' Operator und nicht per Mockito) und den Namen entsprechend setzen.
 
Ja das ist durchaus möglich - aber ich verwende ich auch noch andere Controller und da ist das erstellen der Objekte etwas umständlicher.
Daher die Frage ob es nicht doch anders möglich ist
 
Dann verwende eine Assertion, die eben nicht auf Gleichheit (per equals()/hashCode()) prüft, sondern auf Identität. Dir reicht es ja wahrscheinlich aus, zu wissen, dass exakt die gemockten Objekte in der Liste enthalten sind, und nicht, dass dort Objekte sind, die equals() zu deinen Associations/Objekten sind.
 

Zurück
Oben