0

Can I use Hamcrest's assertThat to assert something about several objects in one line of code? The objects are not in a collection. For instance, I would like to assertThat four strings are equal. Also, how can I assert that all objects are null?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • I found it difficult to read your original question. I've rewritten it, hope it still makes sense to you. If not, please edit it again (or let me know in a comment). – Duncan Jones Sep 26 '14 at 09:13
  • possible duplicate of [What is the idiomatic Hamcrest pattern to assert that each element of an iterable matches a given matcher?](http://stackoverflow.com/questions/5985610/what-is-the-idiomatic-hamcrest-pattern-to-assert-that-each-element-of-an-iterabl) – Joe Sep 26 '14 at 12:42

2 Answers2

1

Use Hamcrest's Every

assertThat(listOfStrings, Every.everyItem(CoreMatchers.equalTo("someValue"));
assertThat(listOfStrings, Every.everyItem(CoreMatchers.nullValue());
John B
  • 32,493
  • 6
  • 77
  • 98
-1
assertThat("All values should be equals to origin", origin, allOf(is(str1), is(str2));
assertThat("All values should be null", null, allOf(is(obj1), is(obj2));