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

How to work with junit ExpectedException?

I'm trying to work with ExpectedExceptions for JUnit. I tried already this: public class ExpectedTest { @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void test() { …
Rafael T
  • 15,401
  • 15
  • 83
  • 144
6
votes
2 answers

matcher library for .net

Does any matcher libraries exist for .net? I am talking about a library like the hamcrest library for java...
khebbie
  • 2,490
  • 3
  • 31
  • 53
6
votes
6 answers

mockito better expected exception test using spy

How can I make the 3rd test to check for the existence of cause1 in the message of the exception? I also listed in the first two tests that have drawbacks. First is not checking for the message second needs a lot of boilerplate code. public class…
raisercostin
  • 8,777
  • 5
  • 67
  • 76
6
votes
1 answer

Mockito acts strangely when I assign multiple custom matchers to a single method

I'm wanting to use two custom matchers for a single method. Basically, if I pass the method VALUE_A, I want it to return RESULT_A, and if I pass it VALUE_B, I want it to return RESULT_B. So here's a code excerpt : class IsNonEmpty extends…
sangfroid
  • 3,733
  • 11
  • 38
  • 42
5
votes
1 answer

Recursive SamePropertyValuesAs matcher in hamcrest

I am looking for a hamcrest matcher that behaves the same way as SamePropertyValuesAs but with the added twist that if a property is not a primitive, one of the standard Java classes (string, integer e.t.c.), or a compound of one of those…
Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104
5
votes
3 answers

Hamcrest Matcher compilation difference between Eclipse and javac

I am trying to make use of a custom matcher from hamcrest within the hasItem matcher @Test public void populatesChildCompanies() { final long firstChildId = 2; final String firstChildName = "jim"; final long secondChildId = 3; …
Matt
  • 2,795
  • 2
  • 29
  • 47
5
votes
1 answer

Generic assertThat(ArrayList, hasItems(InstanceOfSomeInterface)) not working

i want to use Hamcrest’s hasItems with an "actual" collection that is an ArrayList on assertThat(ArrayList, hasItems(InstanceOfSomeInterface)) the compiler says: The method assertThat(T,…
dermoritz
  • 12,519
  • 25
  • 97
  • 185
5
votes
2 answers

How to hint type inference when using static imports?

I am using junit with hamcrest in my unit tests and I came across a generics problem: assertThat(collection, empty()); I am aware of type inference not being available this way and that one of the solutions is to give a type hint, but how should I…
Gabriel Ščerbák
  • 18,240
  • 8
  • 37
  • 52
5
votes
1 answer

Hamcrest Matchers.containsInAnyOrder does not work

I am using this dependencies: org.hamcrest hamcrest-all test
Johnyb
  • 980
  • 1
  • 12
  • 27
5
votes
1 answer

org.hamcrest.Matchers for matching different properties simultaneously of an Object

I am trying to match two different properties of an Object by org.hamcrest.Matchers. Here it is: List leaveApplyList = Lambda.select( allLeaveApplyList, Matchers.allOf( Lambda.having( …
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
5
votes
3 answers

Junit: assert that a list contains at least one property that matches some condition

I have a method that will return a list of objects of type MyClass. MyClass has many properties, but I care about type and count. I want to write a test that asserts that the returned list contains at least one element that matches a certain…
ewok
  • 20,148
  • 51
  • 149
  • 254
5
votes
1 answer

TetsNG SoftAssert with Hamcrest matcher

I want to use a Hamcrest matcher inside a TestNG test and with a soft assert specifically. How can I do this? I know that I can use Hamcrest's assertions inside a test like: assertThat(actual, containsInAnyOrder(expected)); But I can't understand…
Vitali Plagov
  • 722
  • 1
  • 12
  • 31
5
votes
3 answers

Is there a simple way to match a field using Hamcrest?

I want to test whether a specific field of an object matches a value I specify. In this case, it's the bucket name inside an S3Bucket object. As far as I can tell, I need to write a custom matcher for this: mockery.checking(new Expectations() {{ …
Kevin Peterson
  • 7,189
  • 5
  • 36
  • 43
5
votes
0 answers

JsonPath check hashMap values are present using Gatling?

I want to compare some in the jsonPath passed as HashMap() data. How can we achieve it in gatling check ? val hashMap = new HashMap[String,String]() hashMap.put("foo", "bar") OR can I simply check something like this…
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92
5
votes
1 answer

Spring MVC Test with Hamcrest and JsonPath: How count the body size? (not count members)

I am working with: Spring MVC Test Hamcrest JsonPath I have the following how a response from the server: MockHttpServletResponse: Status = 200 Error message = null Headers =…
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158