Question

I want to make sure the below bundles list doesn't contain TestFramework bundle using jUnit assert method.

Bundle[] bundles = framework.getBundleContext().getBundles();

So I was wondering whether jUnit provides any such method to compare a bundle with List of Bundles?

Was it helpful?

Solution

Just loop through the array and use assertEquals or assertFalse (assuming you dont want to use ham as mentioned by John B).

for (Bundle current : bundles)
{
    assertFalse(current instanceof TestFramework);
}

OTHER TIPS

Use Hamcrest:

 Assert.assertThat(bundles, 
          CoreMatchers.not(
               IsArrayContaining.hasItemInArray(TestFramework)));

If you use static imports this would be:

 assertThat(bundles, not(hasItemInArray(myItem)));

If you need instance of you can use:

 Assert.assertThat(bundles, 
          CoreMatchers.not(
               IsArrayContaining.hasItemInArray(
                    CoreMatchers.instanceOf(TestFramework.class))));

IsArrayContaining

FYI, I used Assert.assertThat just because it is easier but I prefer to use MatcherAssert.assertThat because it gives better error documentation.

Edit in response to DwB. I prefer Hamcrest because the non-Hamcrest solution presented works but in the failure case you don't get any information other than expected false got true. This won't tell you which element of the array fails the condition or if multiple elements failed. Once you go Hamcrest you will never go back. I never use assertTrue, assertFalse or assertEquals any more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top