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

Mockito verify Collection of String

I want to verify that a exact Collection is passed to a mocked method. This is how I tried to do it: This is a simple example of my real code, which reproduce exactly the same problem. import com.google.common.collect.Lists; import…
b1zzu
  • 370
  • 4
  • 17
3
votes
1 answer

Hamcrest to match Map with String array value

Is there an elegant way to assert all entries of a map where the values in the Map are String arrays? Matchers.equals seems to check array equality rather than equality based on the contents of the arrays: Map x = new…
Andy Cribbens
  • 1,370
  • 2
  • 11
  • 22
3
votes
1 answer

In Espresso how can I test if *any* child of RecyclerView contains correct text

I have a recycler view which should contain 3 items in an unknown order. I know how to get a recycled item by position and how to check for text onView(withId(...)).check(matches(atPosition(0, hasDescendant(withText(A))))); But I don't know how I…
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
3
votes
1 answer

MockMvc + Hamcrest: inconsistent collections for floating point numbers

I would like to implement a test that checks that my filter for a floating point variable works well. I send the filter and expect the collection to have values only below the defined value. final Double orderPrice = 0.0; …
user1921819
  • 3,290
  • 2
  • 22
  • 28
3
votes
2 answers

Why Hamcrest's containsInAnyOrder matcher accepts array and not a list?

I'm using Hamcrest's containsInAnyOrder matcher when asserting REST response using Rest Assured. Here's an example of my assertion: assertThat( body.jsonPath().getList("zones.name"), …
Vitali Plagov
  • 722
  • 1
  • 12
  • 31
3
votes
1 answer

Mockito: verify parameter without having to cast it

I have a service class with the following method: void doSomething(List list) I mock this class and I want to verify that a list which is passed as a parameter has only one element. I do it like…
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
3
votes
1 answer

cannot access org.hamcrest.Matcher class file for org.hamcrest.Matcher not found in Eclipse and Rest Assured

Hi I'm a newbie for rest assured and trying to do it in BDD, For that I have written a script while trying to run , I got below mentioned Error [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR]…
ArunBharath
  • 143
  • 2
  • 17
3
votes
1 answer

Expected: null but: was <[null]> - Hamcrest and jsonPath

I would like to assert json output from rest controller, but I get "Expected: null but: was <[null]>". This is my testing code: mockMvc.perform(post(TEST_ENDPOINT) .param("someParam", SOMEPARAM) .andDo(print()) …
Bartek
  • 2,109
  • 6
  • 29
  • 40
3
votes
1 answer

Testing type matching differences in Java 6 and Java 8

I updated an application JDK from 1.6 to 1.8 but I kept the language level 1.6 in IntelliJ. After updating I got compilation error in some tests in assertThat statements with checking type of an object. The code is like this: assertThat((Class)…
Govan
  • 2,079
  • 4
  • 31
  • 53
3
votes
1 answer

Spring JsonPath contains in any order exception

I'm writing some integration tests with spring, and wanna check, that json from response contains all required data. See code: @Test public void getAll() throws Exception { String url = "/permissions/all"; int size = 4; try { …
Bohdan Petrenko
  • 997
  • 2
  • 17
  • 34
3
votes
2 answers

how to use hamcrest to compare text ignoring tabs?

I have a test, I want to assert its result: assertThat(cofmanString, new IsEqualIgnoringCase(FileUtils.readFileToString(new File("/Users/myFile.txt")))); in Intellij I see the strings are identical including tabs and…
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
3
votes
3 answers

Assert List> contains List with no order

I have a List> with sample data like: ("T1#R1", "T1#R1", "T1#R3", "T1#R4") ("T1#R1", "T1#R1", "T1#R3", "T1#R5") ("T1#R1", "T1#R1", "T1#R6", "T1#R4") ("T1#R1", "T1#R1", "T1#R6", "T1#R5") And I need to assert, that a List is…
Manuel S.
  • 411
  • 8
  • 21
3
votes
1 answer

JUnit compare objects in collections except specified fields in contained objects

JUnit tests... It needs to compare 2 single elements excepting some fields. I can use assertj for this: Assertions.assertThat(actual).isEqualToIgnoringGivenFields(except, "id", "innerCollection"); and it works good. But it's not enough. I need to…
Sergii
  • 7,044
  • 14
  • 58
  • 116
3
votes
2 answers

Look for a property in List of object using Hamcrest matcher

I have a Java POJO. public class Emp { private String name; private int id; } I have a list of employees (target): List target = new ArrayList(); // Add emp object in list assertThat(target, containsInAnyOrder(????)); In…
rishi
  • 1,792
  • 5
  • 31
  • 63
3
votes
1 answer

REST Assured: How to find element using nested property in JSON and validate other properties

I have limited experience with rest assured. We have a number of tests that I can usually find examples within, or failing that google, but I am stuck when trying to match on a nested property for an element in an anonymous array, and verify…
Clarkey
  • 1,553
  • 5
  • 22
  • 34