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

Why mockito returns error eve argument are identical?

Take a look at this picture. what is going on with Mockito verifying Argument ? I want to verify that view is invoked with the argument factory. @Test public void shouldInitializeTheDriverWithTheRequestFactory() { CVProxy proxy =…
Adelin
  • 18,144
  • 26
  • 115
  • 175
4
votes
1 answer

Can't make JUnit work (assertThat/hasItem)

I've just created a dummy Maven project:
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
4
votes
2 answers

Configure autocomplete in intellij with hamcrest and mockito dsl

I'm wondering if its possible to configure Intellij's smart insert to suggest hamcrest and mockito dsl idioms when in test classes. Really I'm looking for something like eclipse's 'static favorites', so that when I hit ctrl + space or ctrl +shift…
sgargan
  • 12,208
  • 9
  • 32
  • 38
4
votes
1 answer

What's new in Hamcrest 1.2?

What's the difference between Hamcrest 1.1 and 1.2? I couldn't find a release notes page or a file listing changes! Where can I find such info?
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
3
votes
2 answers

Using assertArrayEquals() with wildcards?

I want to test code that produces byte arrays used to send as UDP packets. Although I'm not able to reproduce every byte in my test (e.g. random bytes, timestamps), I'd like to test the bytes that I can predetermine. Is something like the following…
Koraktor
  • 41,357
  • 10
  • 69
  • 99
3
votes
0 answers

Getting "JSON path ... doesn't match" when running REST Assured custom Hamcrest matcher

I am new to REST Assured and my objective is to create a custom Hamcrest Matcher which accepts java object(Item; as referred below) created by me. This is the class that extends the TypeSafeMatcher abstract class. import net.example.Item; import…
kaweesha
  • 803
  • 6
  • 16
3
votes
0 answers

java.lang.NoSuchMethodError: org.hamcrest.beans.SamePropertyValuesAs.isNotNull(Ljava/lang/Object;Lorg/hamcrest/Description;)

I am facing this issue when I am trying to run the tests using intelliJ (Version 11.0.5) only.(macos). When i run tests using Gradle commands it works fine though. I noticed I am able to run these test successfully with older gradle dependency for…
3
votes
2 answers

Is there a `not` ArgumentMatcher for mockito stubbing

I am trying to verify that a method is called with a long having any value but a given one. I thus would like to know if there is an ArgumentMatcher that fits to my use case, such…
EnzoMolion
  • 949
  • 8
  • 25
3
votes
0 answers

Is there any equivalent to ScalaTest's withClue in Junit5 /Hamcrest?

I would like to provide the same textual message for a group of assertions, something like import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @Test public void myTest() { MyClass result = ... …
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151
3
votes
1 answer

Java Hamcrest Matcher check if list contains another list

I want to compare if a list is a sublist of another list Assume I have the following lists List checkList = Arrays.asList("a", "d"); List actualList = Arrays.asList("a", "b", "c", "d"); I want to check if actualList contains…
Madhan
  • 5,750
  • 4
  • 28
  • 61
3
votes
4 answers

Best Way to Assert That Each String in a Collection Contains a Substring?

What is the best way to assert that each element in a collection of strings contains a particular substring? Something like List list = Arrays.asList("xyfoobar", "foobarxy", "xyfoobarxy"); assertThat(list, eachElementContains("foobar")); //…
Marteng
  • 1,179
  • 13
  • 31
3
votes
2 answers

Assert.fail equivalent using hamcrest

I use JUnit for Assert.fail but I do not know what is the Hamcrest equivalent. Does anyone know?
BruceyBandit
  • 3,978
  • 19
  • 72
  • 144
3
votes
1 answer

hasItems Assertion error with iterable containing

I am trying to assert that a list return the value that I need, but the assertion is failing and I don't know why if the difference is…
3
votes
1 answer

JUnit - java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

I am using Win 7 and downloaded Junit 4.13 and hamcrest 2.1 from https://github.com/junit-team/junit4/wiki/Download-and-Install and put them in a folder named JUnit under C. I have added the variable: JUNIT_HOME with value C:\JUnit. I have also…
User 19826
  • 509
  • 2
  • 5
  • 13
3
votes
1 answer

JDK9 Automatic Modules and "Split Packages" Dependencies

I am converting a java project to use modules. One of my modules depends on hamcrest library, and it needs both of the jar files hamcrest.core and hamcrest.library. These two jar files both have org.hamcrest package. So when my module wants to treat…
Feri
  • 1,071
  • 7
  • 19