收集参考

时间:2014-03-18 20:52:14

标签: 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的

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

[成分{name = ingredient1,数量= 0.25Kg,id = 1},成分{name = ingredient2,quantity = 0.3L,id = 2},成分{name = ingredient1,quantity = 0.25Kg,id = 3 },成分{name = ingredient2,quantity = 0.3L,id = 4}]

[成分{name = ingredient1,数量= 0.25Kg,id = 1},成分{name = ingredient2,quantity = 0.3L,id = 2},成分{name = ingredient1,quantity = 0.25Kg,id = 3 },成分{name = ingredient2,quantity = 0.3L,id = 4}]

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

你好

对于模糊的标题感到抱歉,但我不知道如何改写它。

问题 - 问题 目前我收到的消息是:

  

预期:&lt; [成分{name = ingredient1,数量= 0.25Kg,id = 1},成分{name = ingredient2,quantity = 0.3L,id = 2},成分{name = ingredient1,quantity = 0.25Kg ,id = 3}]&gt;但是:&lt; [成分{名称=成分1,数量= 0.25Kg,id = 1},成分{名称=成分1,数量= 0.25Kg,id = 3}]&gt;

所以代码有效,但我的测试子句没有,据我所知,bug在Collection<Ingredient> expected = new ArrayList<Ingredient>(this.validShoppingFacade.getAllIngredients());内,因为预期的列表只是对Facade中列表的引用。如果我要在facde列表上运行for循环以将它们添加到我预期的列表中它会起作用,但对于“测试用例”来说这似乎很奇怪。

2 个答案:

答案 0 :(得分:1)

问题可能出在(List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions)

最后一个印刷语句应打印

[成分{name = ingredient1,数量= 0.25Kg,id = 1},成分{name = ingredient2,quantity = 0.3L,id = 2},成分{name = ingredient1,quantity = 0.25Kg,id = 3 }]

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

缺少id = 3对象。你可以发布getAllIngredientsExcept()方法实现吗?

答案 1 :(得分:0)

assertEquals将调用Object.equals(),它将查看两个列表是否是相同的指针。也许试试assertTrue(org.apache.commons.collections.CollectionUtils.isEqualCollection(expected, (List<Ingredient>)validShoppingFacade.getAllIngredientsExcept(ingredientExceptions))