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

Difference between onView(allOf(withId(R.id.login_card_view), isDisplayed())) and check(matches(isDisplayed()))

What is the difference between 1.ViewInteraction v = onView(allOf(withId(R.id.login_card_view), isDisplayed())) and 2.v.check(matches(isDisplayed())) Whats the use of isDisplayed() in 1 if I am doing the same thing in 2?
Shashi Kumar Raja
  • 508
  • 1
  • 8
  • 23
3
votes
1 answer

Android espresso matching intent with bundle inside a bundle

I am trying to match value that is inside a bundle of bundle (see image). I need to verify that that uriString="file:///storage/emulated...." is set. But so far with my code, I can verify upto value[1]="Benefits Card provied by"…
3
votes
2 answers

Using hamcrest to compare a list of objects containing another list of objects

I have the following object graph: Order --> List of Lines --> List of Shipments object The Line object has an attribute called lineNumber. The Shipment object also has an attribute called lineNumber. I don't know why they have it on both, but…
Inxsible
  • 700
  • 5
  • 27
3
votes
1 answer

How should I handle exceptions in custom Hamcrest matchers?

I am using JUnit and Hamcrest to do some automated testing. To make my tests more readable I want to make a custom matcher however the code that I am calling in the matchesSafely method might throw exceptions. I am unsure of how to handle such…
Silwing
  • 330
  • 2
  • 11
3
votes
1 answer

Spring MVC Test with Hamcrest: how count and test the properties number/size of an object inside a Model

For Spring MVC Test (working together with Java Hamcrest): Testing the scenario where is necessary render a jsp file with a Model object which only contains an instance of the Person class I have the following (works fine):…
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
3
votes
2 answers

How to assert that a list has at least n items which are greater than x (with hamcrest in junit)

I could with following code check if a list has a item, which greater than 30. //Using Hamcrest List ints= Arrays.asList(22,33,44,55); assertThat(ints,hasItem(greaterThan(30))); But how could I assert if a list has at least 2 items, which…
Ophiuchus Hahn
  • 145
  • 2
  • 12
3
votes
3 answers

Java generics and wildcards: How to make this code compile?

I'm writing some matchers using the Hamcrest 1.2 library, but I'm having a hard time with Java wildcards. When I try to compile the following code public class GenericsTest { public void doesNotCompile() { Container container =…
Esko Luontola
  • 73,184
  • 17
  • 117
  • 128
3
votes
2 answers

JUnit Assert#assertSame equivalent in Hamcrest

Is there an equivalent in hamcrest library for JUnit's Assert#assertSame? If yes, what is it? At the moment I can only think of Hamcrest#sameInstance but I am not quite sure this method is the right one to use.
Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63
3
votes
2 answers

Using jUnit 4.12 and Hamcrest 1.3, how can I assert a function returns one of two integers?

I'm interested in writing a test that allows me to check whether a function returns one of two values. For example: @Test public void testRandomFunction() { assertEquals( either(equalTo(2)).or(equalTo(3)), RandomFunction(5) …
RodCardenas
  • 585
  • 6
  • 14
3
votes
2 answers

How use matchers for collection with Hamcrest?

The input: Collection with MyElement without equals method. A org.hamcrest.TypeSafeMatcher implementation, which matches element by some field. The goal is make following statement compilable: Collection elements =…
Cherry
  • 31,309
  • 66
  • 224
  • 364
3
votes
2 answers

How to use hamcrest contains to compare 2 lists?

Why does this test fail? I know contains works when you pass in individual strings separated by commas but I wanted to see if it's possible to just pass in an entire list of strings instead. I just want to make sure that list 1 contains all of the…
Charles H
  • 625
  • 1
  • 7
  • 11
3
votes
1 answer

Multiple hasProperty constraints for Hamcrest matcher

I tried the following code, for matching a single ComplexObject in a List assertThat(complexObjectList, Matchers.hasItems( hasProperty("lang", equalTo(lang)), hasProperty("name", equalTo(name)), hasProperty("desc",…
buræquete
  • 14,226
  • 4
  • 44
  • 89
3
votes
4 answers

How do I assert that a List contains exactly one instance of a particular class?

I'd like to test that a list contains instances of an object. For instance, with a single instance: assertThat(mylist).containsExactly(Matchers.any(ExpectedType.class)); The array returned from tested obj does contain exactly one object of instance…
rds
  • 26,253
  • 19
  • 107
  • 134
3
votes
2 answers

how to match header location response with regular expression using rest-assured

I was making a rest-assured test for testing url redirects using testng. I would like to match header location response to match with regular expression. I am trying to create following method but I didn't find any regular expression matcher using…
rmsorPth
  • 111
  • 2
  • 11