How would you refactor the following if the products can be returned in any order?
List<Product> products = get_products("test_produc");
assertEquals(products.size(),3);
assertEquals(products.get(0).getName(), "test_product1");
assertEquals(products.get(1).getName(), "test_product2");
assertEquals(products.get(2).getName(), "test_produc3");
If it can be done elegantly using streams then I'm oopen to such suggestions. Hamcrest suggestions are also welcome.
List
contains a specific set of elements in any order. That is literally the same thing as asserting two lists contain the same elements ignoring order. How in the world is this question different? Better yet, explain why the answers in that question don't solve your problem. Because the answers over there are exactly the solutions that popped to mind when I read this question. Do you really think you're the first person to have this problem since the creation of JUnit? This is common in unit testing; of course there would be an existing post. – Columniationset
comparison is surely the right solution, and conveys your intent better (you are comparing unordered data (i.e. a set)). This alternative makes the assumption that your data can be ordered, which could bite you in the long run. – Herodias