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

Android Studio - Program type already present: org.hamcrest.CoreMatchers

I have no idea why this error exists: Program type already present: org.hamcrest.CoreMatchers Message{kind=ERROR, text=Program type already present: org.hamcrest.CoreMatchers, sources=[Unknown source file], tool name=Optional.of(D8)} My code in in…
ABCman
  • 125
  • 4
  • 12
10
votes
4 answers

Hamcrest - Elegant way to test complex object with samepropertyvaluesas

I have quite complex object structure (with bunch of primitive fields and object references) and want to test all fields except -a few- of them. As an example; ComplexObject actual = generateMagically("someInput"); ComplexObject expected =…
tugcem
  • 1,078
  • 3
  • 14
  • 25
10
votes
2 answers

Hamcrest matcher comparing double value from JSON

I'm using the Hamcrest CoreMatcher classes as part of spring-test integration tests. My JSON looks like: {"data":[{"distanceInMiles":4,"id":"f97236ba-f4ef-4... And my integration test looks like: double miles = 4.0 Activity a = new…
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
10
votes
3 answers

Hamcrest matcher with slashes is interpreted as a part of validation

I have the following validation where I have to check if returned body has a string containing "id": 6354, but it interprets slashes of special characters. How I can validate strings which contain double quotation marks ? Code import static…
ashur
  • 4,177
  • 14
  • 53
  • 85
10
votes
2 answers

How to use (primitive) autoboxing/widening with Hamcrest?

I came across https://code.google.com/p/hamcrest/issues/detail?id=130 to add some sugar syntax for Hamcrest matchers. But the idea was rejected by the Hamcrest developers. Any other smart ideas to make tests better readable by avoiding having to…
Marcel Overdijk
  • 11,041
  • 17
  • 71
  • 110
10
votes
1 answer

Assert that the list is not empty - with or without Hamcrest?

This is related to Checking that a List is not empty in Hamcrest I have a question over that - If we can assert the list is not empty without using Hamcrest and just using JUnit as: assertFalse(list.isEmpty()); Is using assertThat(list.isEmpty(),…
unknown_boundaries
  • 1,482
  • 3
  • 25
  • 47
10
votes
2 answers

Interrogation about Spring MVC test API's model().attribute() method

I am trying to test the following controller method using the Spring MVC test API: @RequestMapping(value = "/preference/email", method = RequestMethod.GET, produces = "text/html") public String emailForm(@ModelAttribute EmailInfo emailInfo, Model…
balteo
  • 23,602
  • 63
  • 219
  • 412
10
votes
3 answers

Hamcrest and ScalaTest

I found Hamcrest convenient to use with JUnit. Now I am going to use ScalaTest. I know I can use Hamcrest but I wonder if I really should. Does not ScalaTest provide similar functionality ? Is there any other Scala library for that purpose…
Michael
  • 41,026
  • 70
  • 193
  • 341
10
votes
1 answer

Misuse of hamcrest hasItems

I have a list of Integers (current) and I want to check whether this list contains all elements from list expected and not even one element from list notExpected, so code looks like: List expected= new ArrayList(); …
matlockx
  • 425
  • 5
  • 11
9
votes
4 answers

Using a struct with OCMock or Hamcrest

I'm hitting a road block and I'm wondering if the brilliant collective minds here can help. In ObjC CocoaTouch I'm trying to mock an object that takes struct parameters and returns a struct. OCMock is coughing up a hair-ball so I tried wrapping with…
Cliff
  • 10,586
  • 7
  • 61
  • 102
9
votes
4 answers

Standard Hamcrest matcher to check if collection is empty or null?

Is there a shorter version of the following assert statement using standard Hamcrest matchers? Collection collection = ... assertThat(collection, is(anyOf(nullValue(Collection.class), emptyCollectionOf(Element.class)))); I realize…
Altair7852
  • 1,226
  • 1
  • 14
  • 23
9
votes
7 answers

AssertEquals when list content is unordered

How would you refactor the following if the products can be returned in any order? List products = get_products("test_produc"); assertEquals(products.size(),3); assertEquals(products.get(0).getName(),…
Baz
  • 12,713
  • 38
  • 145
  • 268
9
votes
7 answers

How to compare two lists of double in JUnit

In a JUnit 4 test, I have a method getValues() that returns a List object that I want to compare with a reference list. Up to now, the best solution I've found was to use org.hamcrest.collection.IsArray.hasItems and…
Francois
  • 586
  • 2
  • 6
  • 19
9
votes
1 answer

Can the output of the "but: was" clause be customised?

I am trying to write a custom matcher that navigates deep down the object graph to check an important property. assertThat( writeRequest, hasRole("admin") ); When this fails I'd like to see a message like: Expected: "admin" but: was…
Sled
  • 18,541
  • 27
  • 119
  • 168
9
votes
1 answer

How do I match a Class against a specific Class instance in a Hamcrest Matcher?

I would like to be able to assert that the annotation value matches the expected class: import org.junit.Test; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; public final class AnnotatedClassTest { …
Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84