Questions tagged [hamcrest]

Hamcrest is an open source library of constraint classes used to match objects and values, typically by other frameworks such as unit testing, mocking, or collections.

Hamcrest has been ported to Java, C++, Objective-C, Python, PHP and Erlang.

It is included as part of JUnit to make assertions more readable (It is also called fluent API). Compare

assertNotEquals(-1, userName.indexOf("bob"));

to

assertThat(userName, containsString("bob"));
710 questions
-2
votes
2 answers

assertThat: how to invert containsString

I have an assertion that checks if a string is present in the currently selected text: import static org.junit.Assert.assertThat; import static org.hamcrest.Matchers.containsString; assertThat(latest.getText(), containsString(targetString)); But I…
ruud
  • 743
  • 13
  • 22
-2
votes
2 answers

Junit lookup from a list and assert their order

Say I have a list of apple "listOfApples". class apple { int serial; int price; ...... } Multiple apple may have same serial. I have to make sure that all the apples in listOfApples are sorted based on their serial. More precisely all…
neelrotno
  • 159
  • 2
  • 8
-2
votes
1 answer

why do I get 'actual and formal argument lists differ in length' when comparing items and not lists?

I try to run this a junit test which finally asserts: ResponseExample responseExample = new ResponseExample(); responseExample.value = 4; assertThat(comparisonResult.latencyDelta.best.get(0),equalTo(responseExample)); where public…
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
-3
votes
1 answer

Any Hamcrest matcher to check if a collection is composed exactly of x,y elements, unregardless of order and cardinality

I am using hamcrest Matchers and I don't find contains, containsInAnyOrder, hasItems and containsInRelativeOrder solve that need. I need a matcher like: Collection B is composed of exactly the same elements as Collection A in any order and…
Whimusical
  • 6,401
  • 11
  • 62
  • 105
-11
votes
1 answer

What is method "is" used for? What java library does it require?

Here is an example of is method: assertThat(dynamicType.newInstance().toString(), is("Hello World!"));
1 2 3
47
48