1

I've data in mongo which I need to validate:

"items" : [
            {
                    "item" : 0,
            },
            {
                    "item" : 1,
            }
    ],

There's a for loop in my code :

for (Object a : getItems()){
 HashMap<?, ?> b = (HashMap<?, ?>)a;
 assertTrue(b.containsValue(0));
}

There is a problem here as items has value 1 and 0 and I need to assert for the second iteration if it contains 1 too. How do I validate if both 1 and 0 are present?

Is there any other assert method which can do this?

Edited:

List lt1 = new ArrayList();
lt1.add(0);
lt1.add(1);
List lt2 = new ArrayList();

for (Object a : getItems()){
 HashMap b = (HashMap)a;
 lt2.add(b.get("item");
}

assertThat(lt2, hasItems(lt1));

This throws an Invocation Target Exception at assertThat.. line. Also, the JUNIT shows an Assertion Error saying something like below :

Expected : A collection of [0,1]
Got : [0,1]
Mercenary
  • 2,106
  • 6
  • 34
  • 43
  • it depends on hot the hash map is created. you can use a hash map where a key maps to a list, and then assert that the list contains both 0 and 1 – gefei Jan 03 '13 at 08:23
  • 1
    Since the keys are the same, I would have thought that the last value replaces the previous one(s). I suggest you use a List of "items" rather than a Map (or make the keys unique) – Peter Lawrey Jan 03 '13 at 08:45
  • Yes, exactly. And I cannot make the keys unique. I can make a list and add 0 and 1. But, how do I validate them inside the for loop which takes only 1 value at a time? – Mercenary Jan 03 '13 at 08:51

2 Answers2

1

Since you are using JUnit, as of v4.4 the library can use the hamcrest matcher library which offers a rich DSL for building test expressions. This means you can remove the loop entirely and write a single assertion, testing for the existence of all expected values.

For example, hamcrest has a built-in function hasItems() (documentation link for v1.3.RC2 but v1.3 is out - sorry couldn't find an up to date link).

import java.util.List;
import java.util.Arrays;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertThat;

@Test
public void bothValuesShouldBePresent() {
    List<Integer> itemValues = Arrays.asList(new Integer[]{ 0, 1, 2, 3 });
    Integer[] expected = { 0, 1 };
    assertThat(itemValues, hasItems(expected));
}

This, of course, assumes you can modify your getItems() method to return a simple List<Integer>.

Finally, depending on the version of JUnit you are using, hamcrest may or may not be bundled. JUnit inlined hamcrest-core between v4.4 and v4.10. Since it was just the hamcrest-core, I explicitly added the hamcrest-all dependency in my projects. As of JUnit v4.11 hamcrest is no longer inlined (much better IMHO) so you will always need to explicitly add the dependency if you want to use the matchers.

Also, here is a useful blog post on hamcrest collection matching.

Edit:

I have tried to think of what your getItems() might return and here is an updated test example. Note that you need to turn the expected value into an array - see Why doesn't this code attempting to use Hamcrest's hasItems compile?

@Test
public void bothValuesShouldBePresent() {
    List lt1 = new ArrayList();
    lt1.add(0);
    lt1.add(1);
    List lt2 = new ArrayList();

    List fakeGetItems = new ArrayList() {{ add(new HashMap<String, Integer>() {{ put("item", 0); }}); add(new HashMap<String, Integer>() {{ put("item", 1); }} ); }};

    for (Object a : fakeGetItems) {
     HashMap b = (HashMap)a;
     lt2.add(b.get("item"));
    }

    assertThat(lt2, hasItems(lt1.toArray(new Integer[lt1.size()])));
}
Community
  • 1
  • 1
andyb
  • 43,435
  • 12
  • 121
  • 150
  • Thanks. I tried with this with two lists. List a has [0, 1] and list b has the same values. But, when I do `assertThat(a, hasItems(b))`, i get an invoking target exception. Any solutions? – Mercenary Jan 03 '13 at 12:25
  • Without the exact error and source code that might be difficult to answer. Can you add the first few lines of the exception as a comment please? Also, could you also add the simple test in my answer to your code, just to see if that works? – andyb Jan 03 '13 at 13:26
  • 1
    I've edited my own question with the new code under Edited. Could you take a look at that? – Mercenary Jan 03 '13 at 16:19
  • 1
    I've updated my answer with a further example that I hope helps. I couldn't reproduce the exact error you are seeing as I don't have access to the `getItems()` function. – andyb Jan 03 '13 at 17:17
0

This is much easier with AssertJ

@Test
public void bothValuesShouldBePresent1()  {
  List<Integer> lt2 = Arrays.asList(0, 1);
  List<Integer> lt1 = Arrays.asList(0, 1);
  assertThat(lt2).containsExactlyElementsOf(lt1);
}

@Test
public void bothValuesShouldBePresent2()  {
  List<Integer> lt2 = Arrays.asList(0, 1);
  assertThat(lt2).containsExactly(0, 1);
}

@Test
public void bothValuesShouldBePresent3()  {
  List<MyItem> lt2 = Arrays.asList(new MyItem(0), new MyItem(1));
  assertThat(extractProperty("item").from(lt2))
      .containsExactly(0, 1);
}
MariuszS
  • 30,646
  • 12
  • 114
  • 155