/ / Référence de collection - java, collections, référence

Référence de collection - java, collections, 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

`[Ingrédient {nom = ingrédient1, quantité = 0,25 kg, id = 1}, ingrédient {nom = ingrédient2, quantité = 0,3 L, id = 2}]

[Ingrédient {nom = ingrédient1, quantité = 0.25 kg, id = 1}, ingrédient {nom = ingrédient2, quantité = 0,3 L, id = 2}, ingrédient {nom = ingrédient1, quantité = 0,25 kg, id = 3}, ingrédient {nom = ingrédient2, quantité = 0,3 l id = 4}]

[Ingrédient {nom = ingrédient1, quantité = 0.25 kg, id = 1}, ingrédient {nom = ingrédient2, quantité = 0,3 L, id = 2}, ingrédient {nom = ingrédient1, quantité = 0,25 kg, id = 3}, ingrédient {nom = ingrédient2, quantité = 0,3 l id = 4}]

[Ingrédient {nom = ingrédient1, quantité = 0,25 kg, id = 1}, ingrédient {nom = ingrédient1, quantité = 0,25 kg, id = 3}] »

Bonjour

Désolé pour le titre vague mais je ne saurais pas comment le reformuler.

Question - problème actuellement je reçois le message:

attendu:<[Ingrédient {name = ingrédient1, quantité = 0,25 kg, id = 1}, ingrédient {name = ingrédient2, quantité = 0,3 L, id = 2}, ingrédient {name = ingrédient1, quantité = 0,25 kg, id = 3}] > mais était: <[Ingrédient {name = ingrédient1, quantité = 0,25 kg, id = 1}, ingrédient {name = ingrédient1, quantité = 0,25 kg, id = 3}]>

Donc, le code fonctionne mais ma clause de test ne fonctionne pas, pour autant que je sache, le bogue est à l'intérieur Collection<Ingredient> expected = new ArrayList<Ingredient>(this.validShoppingFacade.getAllIngredients()); Puisque la liste attendue n'est qu'une référence àla liste dans la façade. Si je devais exécuter une boucle for sur la liste facde pour les ajouter dans ma liste attendue, cela fonctionnerait, mais cela semble un peu bizarre pour un "cas de test".

Réponses:

1 pour la réponse № 1

Le problème peut résider dans (List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions)

Le dernier relevé d'impression aurait dû s'imprimer

[Ingrédient {nom = ingrédient1, quantité = 0,25 kg, id = 1}, ingrédient {nom = ingrédient2, quantité = 0,3 L, id = 2}, ingrédient {nom = ingrédient1, quantité = 0,25 kg, id = 3}]

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

Il manque un objet id = 3. Pouvez-vous publier l'implémentation de la méthode getAllIngredientsExcept ()?


0 pour la réponse № 2

assertEquals va appeler Object.equals() qui va voir si les deux listes sont le même pointeur. peut-être essayer assertTrue(org.apache.commons.collections.CollectionUtils.isEqualCollection(expected, (List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions))