0

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.

Ahmad
  • 115
  • 7
  • What about doing the same thing you do in the loop but 2 times in a row like when using `hasItem`? –  Dec 31 '14 at 17:53
  • Why would you do `anyOf` in the 2nd approach? `List` is an ordered collection and you know the order of persons, so assert the exact value, maybe by putting the nos. in a new array and using the traditional `for` loop in place of the `for-each` loop, b/c you control the test data and you know the size of the list will be the same as that of the nos. array. – mystarrocks Dec 31 '14 at 18:40
  • @RC. As I know hasItem is used with collection items so while I am looping I am dealing with item-by-item not the collection as an entity. If you have an example to use hasItem with the looping which can solve the issue I will be thankful for you. – Ahmad Dec 31 '14 at 22:57
  • @mystarrocks your suggestion is valid and I thought about it but this will make my test is inflexible and tight to the implementation of my BL how it will order the result. BTW the returned list here is an example, in the real scenario it an object which contains JAXB object containing a List and I am trying to decouple my testing from the implementation as much as I can since the implementation can change in the future and the test still valid. – Ahmad Dec 31 '14 at 23:03
  • possible duplicate of [Is there a simple way to match a field using Hamcrest?](http://stackoverflow.com/questions/5033526/is-there-a-simple-way-to-match-a-field-using-hamcrest) – Joe Jan 01 '15 at 06:31

0 Answers0