List<String> list1 = getListOne();
List<String> list2 = getListTwo();
Run Code Online (Sandbox Code Playgroud)
鉴于上面的代码,我想使用JUnit assertThat()语句来声明它list1是空的还是list1包含的所有元素list2.在assertTrue这个相当于是:
assertTrue(list1.isEmpty() || list1.containsAll(list2)).
如何将此表述为assertThat声明?
谢谢.
您可以通过以下方式执行此操作:
// Imports
import static org.hamcrest.CoreMatchers.either;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.collection.IsEmptyIterable.emptyIterableOf;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;
// First solution
assertThat(list1,
either(emptyIterableOf(String.class))
.or(hasItems(list2.toArray(new String[list2.size()]))));
// Second solution, this will work ONLY IF both lists have items in the same order.
assertThat(list1,
either(emptyIterableOf(String.class))
.or(is((Iterable<String>) list2)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12851 次 |
| 最近记录: |