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

Duplicate class org.hamcrest.BaseDescription found in modules jetified-hamcrest-core-1.3.jar

Android studio 3.6 app/build.gradle: androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'com.azimolabs.conditionwatcher:conditionwatcher:0.2' // Espresso framework androidTestImplementation…
Alexei
  • 14,350
  • 37
  • 121
  • 240
7
votes
2 answers

Hamcrest - Matchers.hasProperty: how to check if a List of objects contains an object with a concrete value

I have the following problem with Hamcrest: I have a List of Employee List employees = hamcrest.getEmployees(); where: public class Employee { private String name; private int age; private double salary; public…
Matley
  • 1,953
  • 4
  • 35
  • 73
7
votes
3 answers

org.hamcrest.Matchers.hasProperty cannot be resolved

The import org.hamcrest.Matchers.hasProperty cannot be resolved in JUnit4.12. What is the alternative to use hasProperty?
krish
  • 95
  • 1
  • 5
7
votes
1 answer

Failing a unit test if an exception is thrown in another thread

Currently, whenever I need to fail a test in response to an exception thrown in another thread, I write something like this: package com.example; import java.util.ArrayList; import java.util.List; import org.testng.annotations.Test; import static…
Bass
  • 4,977
  • 2
  • 36
  • 82
7
votes
2 answers

How to make JUnit4 + Hamcrest 1.3 + Mockito work from Eclipse AND Tycho

I've managed to get JUnit 4.12 + Hamcrest 1.3 + Mockito 2.8.47 to work in Eclipse so that when I add them as dependencies, my tests will run. (The way I've done this is using the p2-maven-plugin to bundle the following artifacts from Maven Central…
s.d
  • 4,017
  • 5
  • 35
  • 65
7
votes
3 answers

Assert that one of string in array contains substring

List expectedStrings = Arrays.asList("link1", "link2"); List strings = Arrays.asList("lalala link1 lalalla", "lalalal link2 lalalla"); For each expectedString, I need assert that any of string in the 'strings' contains…
Hamster
  • 129
  • 3
  • 12
7
votes
1 answer

Using Mockito's argThat() matcher causes "method ambiguous for the type" error

I am trying to mock a method which takes a map and couple other arguments as parameters. My goal is to match the map entries. Originally I defined my mock as: when(discoveryJobCatalogResourceAccessor.findResource(argThat(allOf(hasEntry("start",…
MrkK
  • 873
  • 3
  • 12
  • 22
7
votes
3 answers

Comparing two collections using hamcrest contains() method

I have two collections which I am trying to compare for equality in my unit tests, but I am struggling with the contains method. Here is what I have: @Test public void getAllItems() { Collection actualItems = auction.getAllItems(joe); …
user3248346
7
votes
2 answers

Verify map with custom values

I have the following map: Map My goal is to verify capacity of this map using hamcrest matchers. I've tried the following approach: assertThat(map, hasEntry("key", (MyCustomObject)hasItem(hasProperty("propertyName",…
fashuser
  • 2,152
  • 3
  • 29
  • 51
7
votes
3 answers

Conflicting overloads for Hamcrest matcher

The matcher IsIterableContainingInAnyOrder has two overloads for the static factory method containsInAnyOrder (both have the return type Matcher>): containsInAnyOrder(java.util.Collection>…
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
7
votes
4 answers

Assert that collection "Contains at least one non-null element"

I want to verify that a collection contains at least one non-null element. I have tried is(not(empty())), however this passes in the test below. import org.junit.Test; import java.util.ArrayList; import java.util.Collection; import static…
Rylander
  • 19,449
  • 25
  • 93
  • 144
7
votes
4 answers

Hamcrest's lessThan doesn't compile

Trying to compile this code import static org.hamcrest.Matchers.is; import static org.hamcrest.number.OrderingComparison.lessThan; ... Assert.assertThat(0, is(lessThan(1))); issues this compilation error: assertThat(Object, …
ripper234
  • 222,824
  • 274
  • 634
  • 905
7
votes
2 answers

For method of type T, what should be its 'inferred' type when it takes two arguments?

There is a method in hamcrest library: package org.hamcrest.core ... public static Matcher allOf(Matcher first, Matcher second) { List> matchers = new ArrayList>(2); …
Marcin
  • 4,080
  • 1
  • 27
  • 54
6
votes
1 answer

Hamcrest generics hell #2 : iterableWithSize gives errror "is not applicable for the arguments"

In hamcrest (1.3.RC2, with no JUnit dependencies) I am failing using iterableWithSize(). I have an (extension of) an Iterator parametrized with Content like this EndResult contents = contentRepository.findAllByPropertyValue("title",…
Angelos Pikoulas
  • 1,002
  • 1
  • 14
  • 21
6
votes
2 answers

I can't compile hamcrest hasKey() method

This is the code: Map map; org.hamcrest.MatcherAssert.assertThat(map, org.hamcrest.Matchers.hasKey(new Foo())); This is what compiler is saying: cannot find symbol method…
yegor256
  • 102,010
  • 123
  • 446
  • 597