I have an Entity object lets say Person and a generated JAXB PersonType for the CXF, my service should return a list of PersonType, so I get List of Person entities and then I convert it to PersonType.
In my test case I created a dummy Person objects for testing like this:
Person personOne = new Person();
personOne.setId(10L);
personOne.setName("TestPersonOne");
Person personTwo = new Person();
personTwo.setId(20L);
personTwo.setName("TestPersonTwo);
And then I passed it to my business method to handle it expecting a list of two PersonType JAXB objects.
I tried many ways:
First I tried to use hamcrest hasItem matcher with assertThat, I converted the personOne and PersonTwo to PersonType objects
List<PersonType> personTypes = personService.getAllPersons(personOne, personTwo);
PersonType personTypeOne = PersonUtility.convertToPeronsType(personOne);
PersonType personTypeTwo = PersonUtility.convertToPeronsType(personTwo);
assertThat(personTypes, hasItem(personTypeOne));
assertThat(personTypes, hasItem(personTypeTwo));
But it didn't work because this is using equalts() method in the JAXB object (PersonType) which is not overridden since it is generated from the schema, so it is asserting object references which are apparently different. And I don't have control to override the equals() method.
Second attempt: I tried to loop through the PersonType list and asserting object by object.
for (PesonType personType : personTypes) {
assertThat(Long.parseLong(personType.getId().getValue()), anyOf(equalTo(10L), equalTo(20L)));
}
But this one also is not reliable because if I have a bug in my implementation and it returns the PersonType list with personTwo twice, the assertion will pass because in each iteration anyOf(10L, 20L) will be true.
And this is false positive, I need each iteration to be different so if first one is equatlTo(20L) then next should be equalTo(10L) or it will fail and vice versa. But this assertion doesn't do that.
So how can I achieve this functionality by making each iteration assertion depends on the previous iteration assertion? Or something like that.