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
33
votes
3 answers

Hamcrest - what version to use? 1.3 or 2

I am quite confused. Currently I am testing my spring application using org.springframework.boot spring-boot-starter-test test I was happy as long as I…
ben
  • 502
  • 4
  • 15
33
votes
4 answers

How to use Hamcrest to inspect Map items

I have been recently using Hamcrest library to write some tests and quite successful but now I need to do something more complex and started to see a lot of difficulties. I need to inpsect and verify the properties of the items in a Map. My…
dnang
  • 939
  • 2
  • 12
  • 17
30
votes
3 answers

Mixing Hamcrest and TestNG

Has anyone integrated Hamcrest with TestNG so that its matchers can easily be used in TestNG assertions?
Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
30
votes
6 answers

How to test enum types?

I'm currently trying to build a more or less complete set of unit tests for a small library. Since we want to allow different implementations to exist we want this set of tests to be (a) generic, so that we can re-use it to test the different…
seb
  • 1,800
  • 2
  • 13
  • 10
27
votes
3 answers

Testing content of list ignoring some of the fields

I have a scenario where I receive a list from a method call and I would like to assert that the list contains the correct elements. One way to do this would be to look for some detail in each element to see which expected element to compare with -…
homaxto
  • 5,428
  • 8
  • 37
  • 53
27
votes
7 answers

Why insist all implementations of an interface extend a base class?

I was just looking at the Java Hamcrest code on GitHub, and noticed they employed a strategy that seemed unintuitive and awkward, but it got me wondering if I'm missing something. I noticed in the HamCrest API that there is an interface Matcher and…
James Dunn
  • 8,064
  • 13
  • 53
  • 87
27
votes
3 answers

Using hamcrest matchers with primitive type arrays

Hamcrest works well for primitive data types due to automatic boxing and unboxing like in this case: assertThat(1, is(1)); However, I would like to use hamcrest's hasItemInArray matcher with a primitive type array like this: int[] values =…
k13n
  • 787
  • 1
  • 8
  • 8
26
votes
3 answers

What is the idiomatic Hamcrest pattern to assert that each element of an iterable matches a given matcher?

Examine the following snippet: assertThat( Arrays.asList("1x", "2x", "3x", "4z"), not(hasItem(not(endsWith("x")))) ); This asserts that the list doesn't have an element that doesn't end with "x". This, of course, is the…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
26
votes
6 answers

How to use Hamcrest in Java to test for a exception?

How do I use Hamcrest to test for an exception? According to a comment in https://code.google.com/p/hamcrest/wiki/Tutorial, "Exception handling is provided by Junit 4 using the expected attribute." So I tried this and found that it worked: public…
Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
26
votes
15 answers

Ant + JUnit: NoClassDefFoundError

Ok, I'm frustrated! I've hunted around for a good number of hours and am still stumped. Environment: WinXP, Eclipse Galileo 3.5 (straight install - no extra plugins). So, I have a simple JUnit test. It runs fine from it's internal Eclipse JUnit…
SteveT
  • 959
  • 3
  • 10
  • 17
24
votes
3 answers

Spring MockMvc: match a collection of JSON objects in any order

I have an API endpoint which, when called with GET, returns an array of JSON objects in the body, like this: [ {"id": "321", "created": "2019-03-01", "updated": "2019-03-15"}, {"id": "123", "created": "2019-03-02", "updated": "2019-03-16"} ] I…
Vasiliy Galkin
  • 1,894
  • 1
  • 14
  • 25
24
votes
3 answers

isinstance without importing candidates

We have a function which takes a variety of different types of input: a function, a string, a compiled regular expression, a Hamcrest Matcher, and filters a list appropriately based on the type of the input. We're currently using…
Dragon Dave
  • 637
  • 6
  • 15
23
votes
2 answers

Using not operation in hamcrest

I was recently trying to assert the inequality in one of the test. However I wasnt able to find the appropriate matcher in hamcrest. What I ideally want to do is something like. assertThat(2 , isNot(3)); Is there any way to do it?
Ankit Dhingra
  • 6,534
  • 6
  • 31
  • 34
23
votes
5 answers

Is there any Hamcrest Matcher for java.util.Optional?

I am looking for a Hamcrest Matcher to unit test methods that return a java.util.Optional type. Something like: @Test public void get__Null(){ Optional element = Element.get(null); assertThat( sasi ,…
borjab
  • 11,149
  • 6
  • 71
  • 98
22
votes
3 answers

Is there a way to use AssertJ assertions with Spring MVC Test?

I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers. But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC…
ajm
  • 12,863
  • 58
  • 163
  • 234
1 2
3
47 48