문제

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?

도움이 되었습니까?

해결책

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);
}

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top