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
6
votes
5 answers

Hamcrest Matcher signer information does not match signer information of other classes in the same package

I'm trying to write some integration tests in my Spring Boot application using REST-Assured and JUnit5 but when I run the following: @SpringBootTest(classes = ProductsApplication.class) class ProductsApiTest { @Before public void setup() { …
seamaster
  • 381
  • 1
  • 4
  • 12
6
votes
2 answers

Unresolved reference: Matchers

I am unable to run the unit test when I import org.hamcrest.Matchers as I need lessThan(). My instrumentation tests compile properly while using the greaterThan matcher but not the unit tests Code: import org.hamcrest.CoreMatchers.* import…
jasperagrante
  • 354
  • 4
  • 17
6
votes
3 answers

Matching mutable object without ArgumentCaptor

I have to test a method which uses a mutable object private final List buffer; ... flushBuffer() { sender.send(buffer); buffer.clear(); } I need to test that it sends buffers with exact size. ArgumentCaptor is not applicable because…
Denys Kurochkin
  • 1,360
  • 1
  • 18
  • 34
6
votes
4 answers

Espresso check view either doesNotExist or not isDisplayed

The following statement does not work because doesNotExist() returns a ViewAssertion instead of a matcher. Any way to make it work without a try-catch? .check(either(matches(doesNotExist())).or(matches(not(isDisplayed()))));
ferbeb
  • 163
  • 1
  • 2
  • 11
6
votes
3 answers

Is there a version of JUnit assertThat which uses the Hamcrest 'describeMismatch' functionality?

In every version of JUnit I have tried (up to 4.8.1), a failing assertThat will display an error message that looks like: expected: [describeTo] got: [String representation of object] In other words, it will display the toString() of the object…
Jacob
  • 345
  • 4
  • 11
6
votes
1 answer

Espresso testing that ImageView contains a drawable

I've implemented Daniele Bottilo's Drawable matcher from his medium post. Now I'd like to use it to test that my image view is not empty. I tried this: onView(withId(R.id.image)) .check( matches( not(noDrawable()) ) ); It does not work,…
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
6
votes
3 answers

How to assert that Set has item with exact property with hamcrest

i've been trying assert that my Set has collection with given property with hamcrest, using this solution, but i have : java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V at…
Szympek
  • 183
  • 1
  • 2
  • 7
6
votes
6 answers

Assert collection contains object of custom class, which does not override equals/hashcode

We have a custom class with several fields, for which we cannot override equals/hashcode methods for business domain reasons Nevertheless, during unit testing we should assert on whether a collection contains an item of this class List
hammerfest
  • 2,203
  • 1
  • 20
  • 43
6
votes
1 answer

How to define Method signature when passing a Function in Java - JUNIT with Hamcrest Fails

I have the following function: private Person findMax(List persons, Function compare) { return persons.stream().max(Comparator.comparing(compare)).get(); } I can call it doing: Person result =…
Martin
  • 3,018
  • 1
  • 26
  • 45
6
votes
1 answer

Hamcrest describeMismatchSafely always printing Object.toString() insted of my implementation

I wrote a custom matcher to compare my objects. It all works except for the describeMismatchSafely method. I kept simplyfing and simplyfing, until I got this: public static TypeSafeMatcher equalTo(final IMyObj expected) { return new…
Mauzik
  • 78
  • 6
6
votes
3 answers

How to mock persisting and Entity with Mockito and jUnit

I'm trying to find a way to test my entity using Mockito; This is the simple test method: @Mock private EntityManager em; @Test public void persistArticleWithValidArticleSetsArticleId() { Article article = new Article(); …
Patrick
  • 489
  • 2
  • 8
  • 12
6
votes
2 answers

Is it acceptable to use Hamcrest matchers in non test code

I am trying to find a definitive answer to using Hamcrest matchers in non test code. I have done a bit of research, and have some contrasting quotes: Hamcrest on Wikipedia: Hamcrest is a framework that assists writing software tests in the Java…
acanby
  • 2,936
  • 24
  • 26
6
votes
2 answers

Does JUnit or Hamcrest have tolerance assertions?

I want to do an assertion where the actual value is within either a fixed +/- value of the expected value or a percent +/- value of the expected value. While googling I noticed that NUnit had a nice syntax for that : Assert.That( 5.5, Is.EqualTo( 5…
Krish Srinivasan
  • 568
  • 1
  • 6
  • 14
6
votes
1 answer

hamcrest containsInAnyOrder only working for specific order

I'm trying to make a test that checks if an a certain list has items and I don't care about the order. The way I want to be able to do this is by testing the item has a certain property with a certain value. I've isolated the senario with the…
stefaan dutry
  • 1,096
  • 1
  • 10
  • 21
6
votes
2 answers

How can I get contains(List itemMatchers) to compile in Java 7?

I am learning Hamcrest 1.3 and I want to come up with an example for each Hamcrest static method in Matchers. The Javadoc helpfully already has examples for some methods. I tested the following contains code snippet with Java 8 and it…
BennyMcBenBen
  • 1,438
  • 2
  • 20
  • 37