MrMe TumbsUp
MrMe TumbsUp

Reputation: 416

Collection reference

@Test
    public void test_getAllIngredientsExcept_Parameter_Valid_List()throws DatabaseException, ServiceException, DomainException{
        System.err.println(this.validShoppingFacade.getAllIngredients());
        this.validShoppingFacade.addIngredient(validIngredient);
        Collection<Ingredient> expected = new ArrayList<Ingredient>(this.validShoppingFacade.getAllIngredients());

        this.validShoppingFacade.addIngredient(anotherValidIngredient);
        System.err.println(this.validShoppingFacade.getAllIngredients());
        Collection<Ingredient> ingredientExceptions = new ArrayList<Ingredient>(); 
        ingredientExceptions.add(anotherValidIngredient);

        System.err.println(this.validShoppingFacade.getAllIngredients());
        System.err.println((List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions));
        assertEquals(expected,(List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions));
    }

sys.err's

`[Ingredient{name=ingredient1, quantity=0.25Kg, id=1}, Ingredient{name=ingredient2, quantity=0.3L, id=2}]

[Ingredient{name=ingredient1, quantity=0.25Kg, id=1}, Ingredient{name=ingredient2, quantity=0.3L, id=2}, Ingredient{name=ingredient1, quantity=0.25Kg, id=3}, Ingredient{name=ingredient2, quantity=0.3L, id=4}]

[Ingredient{name=ingredient1, quantity=0.25Kg, id=1}, Ingredient{name=ingredient2, quantity=0.3L, id=2}, Ingredient{name=ingredient1, quantity=0.25Kg, id=3}, Ingredient{name=ingredient2, quantity=0.3L, id=4}]

[Ingredient{name=ingredient1, quantity=0.25Kg, id=1}, Ingredient{name=ingredient1, quantity=0.25Kg, id=3}]`

Hello

Sorry for the vague title but i wouldn't know how else to rephrase it.

Question - problem currently i get the message:

expected:<[Ingredient{name=ingredient1, quantity=0.25Kg, id=1}, Ingredient{name=ingredient2, quantity=0.3L, id=2}, Ingredient{name=ingredient1, quantity=0.25Kg, id=3}]> but was:<[Ingredient{name=ingredient1, quantity=0.25Kg, id=1}, Ingredient{name=ingredient1, quantity=0.25Kg, id=3}]>

So the code works but my test clause doesnt, As far as i know the bug is within Collection<Ingredient> expected = new ArrayList<Ingredient>(this.validShoppingFacade.getAllIngredients()); Since the expected list is just a reference to the list in the Facade. If i was to run a for loop over the facde list to add them in my expected list it would work, but that seems abit wierd for a "test case".

Upvotes: 0

Views: 70

Answers (2)

Kirby
Kirby

Reputation: 15855

assertEquals is going to call Object.equals() which is going to see if the two lists are the same pointer. perhaps try assertTrue(org.apache.commons.collections.CollectionUtils.isEqualCollection(expected, (List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions))

Upvotes: 0

Pankaj Gadge
Pankaj Gadge

Reputation: 2814

The problem may lie in (List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions)

The last print statement should have printed

[Ingredient{name=ingredient1, quantity=0.25Kg, id=1}, Ingredient{name=ingredient2, quantity=0.3L, id=2}, Ingredient{name=ingredient1, quantity=0.25Kg, id=3}]

System.err.println((List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions));

This is missing id=3 object. Can you post the getAllIngredientsExcept() method implementation?

Upvotes: 1

Related Questions