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
66
votes
4 answers

Hamcrest number comparison using between

Is there a way in Hamcrest to compare a number within a number range? I am looking for something like this: assertThat(50L, is(between(12L, 1658L)));
saw303
  • 8,051
  • 7
  • 50
  • 90
63
votes
15 answers

Composer hanging while updating dependencies

I tried updating a Laravel project I'm working on today using composer update But it hung on Updating dependencies (including require-dev) So I tried things like updating composer, dump-autoload, but nothing seemed to work. Then I ran it in verbose…
Chris
  • 4,277
  • 7
  • 40
  • 55
58
votes
5 answers

What is the alternative to using the Deprecated Hamcrest method is()?

I use the following code at the moment to assert on a boolean value, however the method org.hamcrest.Matchers.is() is deprecated. assertThat(someValue, is(false)); Is there a simple alternative syntax to test for boolean values without resorting to…
Brad
  • 15,186
  • 11
  • 60
  • 74
56
votes
9 answers

Map equality using Hamcrest

I'd like to use hamcrest to assert that two maps are equal, i.e. they have the same set of keys pointing to the same values. My current best guess is: assertThat( affA.entrySet(), hasItems( affB.entrySet() ); which gives: The method assertThat(T,…
mo-seph
  • 6,073
  • 9
  • 34
  • 35
56
votes
7 answers

Mockito and Hamcrest: how to verify invocation of Collection argument?

I'm running into a generics problem with Mockito and Hamcrest. Please assume the following interface: public interface Service { void perform(Collection elements); } And the following test snippet: Service service =…
Philipp Jardas
  • 3,222
  • 3
  • 29
  • 42
55
votes
9 answers

Why doesn't this code attempting to use Hamcrest's hasItems compile?

Why does this not compile, oh, what to do? import static org.junit.Assert.assertThat; import static org.junit.matchers.JUnitMatchers.hasItems; ArrayList actual = new ArrayList(); ArrayList expected = new…
ripper234
  • 222,824
  • 274
  • 634
  • 905
53
votes
6 answers

How to check if collection contains items in given order using Hamcrest

How to check using Hamcrest if given collection is containing given items in given order? I tried hasItems but it simply ignores the order. List list = Arrays.asList("foo", "bar", "boo"); assertThat(list, hasItems("foo", "boo")); //I want…
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
52
votes
24 answers

hamcrest tests always fail

I am using hamcrest 1.3 to test my code. It is simply a die. I am trying to test it to make sure the number generated is less than 13. I had a print statement that printed what the number generated was. The number generated was always less than 13…
beachw08
  • 1,427
  • 1
  • 10
  • 8
50
votes
1 answer

Mockito's Matcher vs Hamcrest Matcher?

This is going to be an easy one, but I cannot find the difference between them and which one to use, if I have both the lib's included in my classpath?
tintin
  • 5,676
  • 15
  • 68
  • 97
43
votes
11 answers

How to assertThat String is not empty

Asserting that a string is not empty in junit can be done in the following ways: assertTrue(!string.isEmpty()); assertFalse(string.isEmpty()); assertThat(string.toCharArray(), is(not(emptyArray())); // (although this didn't compile) My question…
Armine
  • 1,675
  • 2
  • 24
  • 40
43
votes
8 answers

NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11

Another instance of the NoSuchMethodError for the JUnit & Hamcrest combination. Offending code: assertThat(dirReader.document(0).getFields(), hasItem( new FeatureMatcher(equalTo("Patisnummer"), "Field key", "Field key")…
Kasper van den Berg
  • 8,951
  • 4
  • 48
  • 70
39
votes
7 answers

Hamcrest Date Matchers

I need to test before/after on dates in a certain test case. I'd like to use Hamcrest matchers if possible. Are there any matchers for Hamcrest (Java) for working with Dates? If so, what package/class would I find the particular date matcher…
smp7d
  • 4,947
  • 2
  • 26
  • 48
36
votes
3 answers

How to verify Map size using Hamcrest

Map> mapMap = new HashMap>(); Currently asserting like this assertThat(mapMap.size(), is(equalTo(1))); Or assertThat(mapMap.values(), hasSize(1)); Are there any other methods like one used…
Ramesh
  • 361
  • 1
  • 3
  • 5
35
votes
2 answers

hamcrest hasItem and hasProperty, assert if a object with property value exists

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.equalTo; assertThat(actual, hasItem(hasProperty("id", equalTo(1L)))); where actual is a POJO with id as Long. I…
wenic
  • 1,169
  • 2
  • 14
  • 23
34
votes
3 answers

How to compile Kotlin unit test code that uses hamcrest 'is'

I want to write a unit test for my Kotlin code and use junit/hamcrest matchers, I want to use the is method, but it is a reserved word in Kotlin. How can I get the following to compile? class testExample{ @Test fun example(){ assertThat(1,…
thecoshman
  • 8,394
  • 8
  • 55
  • 77
1
2
3
47 48