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
4
votes
1 answer

What is the dependency I need to get jsonpath matching to work with Spring mock mvc?

I’m using Hamcrest 2.0.0.0 and trying to get json-path matching to work using Spring (v 3.2.11.RELEASE)’s mockmvc framework. I have this in my JUnit (v 4.11) test mockMvc.perform(get("/api/users/" + id)) .andExpect(status().isOk()) …
Dave
  • 15,639
  • 133
  • 442
  • 830
4
votes
1 answer

Using hamcrest for comparing each item in two separate lists with own matcher

i try to compare two lists with each other: ListA (a1,a2,a3,...) ListB (b1,b2,b3,...) I want that a1 is compared to b1, a2 to b2, a3 to b3, .... But i have to use another method and cannot use .equals! I have written my own hamcrest matcher. But i…
Dr. Simon Harrer
  • 1,954
  • 1
  • 15
  • 26
4
votes
1 answer

Hamcrest assertThat - type inference

Here's a simplified version of what's being done in one of my projects: List names = ... assertThat(names, is(empty())); This works just fine on my Eclipse running on Java 1.7.0.79 (and on 1.6.0.31). However the compilation fails on a…
mystarrocks
  • 4,040
  • 2
  • 36
  • 61
4
votes
2 answers

stringContainsInAnyOrder matcher in Hamcrest?

There's a StringContainsInOrder Matcher in Hamcrest. How can I assert that a String contains a collection of Strings in any order?
brabec
  • 4,632
  • 8
  • 37
  • 63
4
votes
4 answers

Mockito/JMockit & Hamcrest matchers : How to verify Lists/Collections?

This 2013 post on SO asked how to use Hamcrest matchers to verify lists/collections invocations in Mockito. The accepted solution was to cast the Matcher to a (Collection). I'm trying to do something similar, but running into a class cast error. …
Eric B.
  • 23,425
  • 50
  • 169
  • 316
4
votes
7 answers

How to resolve hamcrest jar file error in creating new project in android studio

I got this error while creating new project in android studio,I have checked the library and have hamcrest-core-1.3.jar file .Please help me Error:A problem occurred configuring project ':app'. > Could not download hamcrest-core.jar…
seeta
  • 41
  • 1
  • 1
  • 4
4
votes
1 answer

Using generic bounded wildcards when testing Maps with junit

I'm trying to understand why this junit assertion is giving me a compile time error: Map> actual = methodToTest(); assertThat(result, hasEntry("foo", new HashSet(Arrays.asList("bar")))); If I write it this way it works…
acvcu
  • 2,476
  • 10
  • 40
  • 56
4
votes
2 answers

Why is it Matcher as return type in Hamcrest instanceOf method

I am having trouble understanding the method signature of the 'instanceOf' method in hamcrest package. Here is the method public static Matcher instanceOf(Class type) { return (Matcher) new IsInstanceOf(type); } I can understand…
xipengyang
  • 96
  • 1
  • 5
4
votes
1 answer

Correct unit testing property holder with no equals implementation

I am having a debate with a colleague about a specific situation that I encountered, and would be great if someone could chime in with some view or theory grounding. Let's say that we have model objects of type A. They are java beans, property…
florin.bunau
  • 1,875
  • 3
  • 17
  • 25
4
votes
1 answer

How to create a custom domain specific assert/matcher in spock or hamcrest

I am trying to write a custom domain related assert/matcher in spock or hamcrest, but I am not sure how to proceed. I tried writing a custom Matcher in hamcrest but so far that has only led me to a partial solution. I am looking for some guidance as…
Pranav Shah
  • 3,233
  • 3
  • 30
  • 47
4
votes
2 answers

NoSuchMethodError with Hamcrest 1.3 and Maven on GAE

I've already found three questions which should relate to the same problem, but somehow they didn't quite solve my problem. NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11 NoSuchMethodError: org.hamcrest.Matchers.hasXPath when I run tests in…
Daniel
  • 85
  • 3
  • 9
4
votes
2 answers

Hamcrest matcher to compare superclass and subclass

I have following classes: abstract class Answer {} class AnswerInt extends Answer {} class AnswerText extends Answer {} Now I'd like to use Hamcrest Matcher in following test (it's just simplified example): @Test public void…
lopisan
  • 7,720
  • 3
  • 37
  • 45
4
votes
1 answer

How to use Hamcrest to test for exception?

I have the following code: def f(String s) { assert !s?.contains('.') } What Hamcrest matcher can be used to test the assertion? I know I can use a try/catch block but I prefer keeping the cyclomatic complexity of tests to one.
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
4
votes
1 answer

Problems with "OR" Matcher in When predicate with Mockito

I want to do this: when(myObject.getEntity(1l,usr.getUserName()).thenReturn(null); when(myObject.getEntity(1l,Constants.ADMIN)).thenReturn(null); in one line with matchers. So, I have this code: import static org.mockito.Matchers.*; import static…
Imaky
  • 1,227
  • 1
  • 16
  • 36
4
votes
1 answer

How to assert that every item in collection is within range

I have a Groovy array that will get a set number of random Integer Values. And I want to assert that each item in the array has a value within the given range. I'm trying to use Hamcrest Matchers. So my test looks like this: @Test void…
TroyB
  • 195
  • 1
  • 14