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

Using Espresso how do i check the number of items in my alert Dialog

I have an alert dialog that displays a number of items. private String[] choices; // populated externally AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setItems(choices, new…
KG -
  • 7,130
  • 12
  • 56
  • 72
5
votes
2 answers

How to test an .equals() method using JUnit and Hamcrest

I have a class List (EDIT: that I wrote myself), with method List.equals, so I want to run something like List list1 = new List(); List list2 = new List(); assertTrue(list1.equals(list2)); So using matchers and assertThat, I thought…
Henry
  • 6,502
  • 2
  • 24
  • 30
5
votes
1 answer

Hamcrest Matcher with junit style diff

I am using Hamcrest Matcher to compare two JSON objects. The compare method uses Gson parser. The matcher works great but when the two JSON are not same, i am only able to show message like: Expected: <[{"data":"data1","application":{"id":"1"}}]> …
rpatali
  • 71
  • 6
5
votes
2 answers

Hamcrest library for date

I have been looking around but I have not found anything useful. Are there any 3rd party libraries that work with hamcrest that have extensive date matching? Specifically I am looking for matchers along the lines of: assertThat(myDate,…
Dan
  • 51
  • 3
4
votes
3 answers

Hamcrest matcher for a String, where the String contains some random values

Is there a way to match the following string with any of the hamcrest matchers. "{\"messageType\":\"identify\",\"_id\":\"7de9a446-2ced-4bda-af35-81e95ad2dc32\",\"address\":\"192.168.0.0\",\"port\":7070}" This string is passed to a method. I use…
Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
4
votes
1 answer

Which Hamcrest matcher to import

I am using Hamcrest in a gradle/JVM project via the org.hamcrest:hamcrest:2.2 artifact which is what the Hamcrest website recommends. When I go to static import a matcher, e.g. is or notNullValue, I am presented with 3 options of where to import it…
hughjdavey
  • 1,122
  • 2
  • 9
  • 17
4
votes
1 answer

How to assert json path value is greater than value?

Given the following JSON that is returned from a REST call: {"Statistik Eintraege": [{"Wert":"1","Anzahl":41}, {"Wert":"","Anzahl":482}, {"Wert":"-3","Anzahl":1}, {"Wert":"-1","Anzahl":3}, {"Wert":"-2","Anzahl":3}], "Statistik…
DerBenniAusA
  • 727
  • 1
  • 7
  • 13
4
votes
1 answer

Test if an array contains an element from another array with Hamcrest

I actually have two types of data: a = ["1", "2", "3", "3", "5"] b = ["7", "2"] given() .header("Content-Type", "application/json"). when() .post(this.url). then() .statusCode(200) .contentType("application/json") …
Betcha
  • 155
  • 3
  • 12
4
votes
3 answers

REST-assured: verify that a JSON object contains all Strings from a List

I have constructed a list that contains Strings of a JSON object's body field names like this: List fieldNames = new ArrayList(); Then I have used REST-assured to GET a response which is in JSON format like this: { "id": 11, …
Clx3
  • 119
  • 1
  • 1
  • 11
4
votes
0 answers

How to check if two StateListDrawable(s) are equal?

I'm trying to write a test to check if my ViewGroup has the right background. I'm using Espresso and Hamcrest for the test. This is the xml file I used for the background of my ViewGroup(RelativeLayout).
4
votes
1 answer

Hamcrest. Match item in collection with 2 specific property values

I have a test with a collection of SpecialObject as result. SpecialObject has "name" and "surname" as properties. I want to test if the collection contains a specialObject with 2 specific properties, "name=myname" and "surname=lastname". Here is…
Slagathor
  • 852
  • 7
  • 23
4
votes
1 answer

JUnit / Hamcrest - org.hamcrest.CoreMatchers.is() is deprecated. What should I use instead?

The method org.hamcrest.CoreMatchers.is() deprecated. The doc says to use - org.hamcrest.CoreMatchers.isA() instead. But isA() seems to serve a different case all together. Ok. What ever, coming to my problem. Earlier I was using is() as below …
samshers
  • 1
  • 6
  • 37
  • 84
4
votes
3 answers

Assert that a list of strings contains a string ignoring case

I'm using Hamcrest matchers to assert that a list of strings contains a string, ignoring the case. My code is as follows: assertThat("myList has the string", myList, Matchers.hasItem(Matchers.equalToIgnoringCase(string))); But my java compiler is…
argo
  • 381
  • 1
  • 5
  • 15
4
votes
2 answers

"no suitable method found" when using hasItem Hamcrest Matcher

When I run the following jUnit test: @Test public void test(){ List> data=new ArrayList<>(); Map map=new HashMap<>(); map.put("x","y"); data.add(map); assertThat(data,…
User1
  • 39,458
  • 69
  • 187
  • 265
4
votes
3 answers

hamcrest core, why need this?

I was using JUnit for my TDD in Java and noticed there are two components to download from JUnit.org. First of all, I thought I need JUnit component and downloaded, installed. When I compiled and tried to run my test, it was complaining about…
Young
  • 73
  • 1
  • 6