Questions tagged [matcher]

Matchers are objects used among other things by testing libraries to check, if an object matches an abstract description of an expected state. Do not use this tag for use of the Matcher class for regular expression matching, use [regex] instead.

The most basic matcher is a function from an object to boolean, where the return value denotes whether the argument matches the description represented by the matcher. Apart from doing the actual matching matchers most of the time also provide some information about why a matcher did not match, and they can be combined using methods or functions, that take matchers as an argument and return new matchers (see the example below).

Matchers allow expectations to be written in an internal DSL. For example using Hamcrest and JUnit assertions can be written like this:

assertThat("nuts", biscuit.nutCount(), allOf(greaterThan(3), evenNumber()));

This is relatively easy to understand, even without knowing how it exactly works, and it will provide failure messages that explain the reason why the assertion failed. In the above example allOf(), greaterThan() and evenNumber() are all methods returning matchers.

Matchers are also used in other libraries, for example mock libraries use matchers in order to formulate expectations about calls to mocks, stubs or spies, like in the following example with Mockito.

when(mockedList.get(anyInt())).thenReturn("element");

Here anyInt() returns a matcher that matches any Integer or int.

458 questions
8
votes
2 answers

Scalatest: how to check if a collection contains element that satisfies certain criteria

Say I have a list of books: val books = List( Book(title="Foo", year=2014), Book(title="Bar", year=2014)) How to check with a single expression if collection books is not empty and only contains books published in 2014?
Alex Vayda
  • 6,154
  • 5
  • 34
  • 50
8
votes
2 answers

how to implement a hamcrest matcher

I want to run this line of code: assertThat(contextPin.get(), equalTo(pinPage.getPinObjFromUi())); but I want to print to the log be informative meaning that I could know which fields were not equal. So I have thought to implement a matcher. I have…
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
7
votes
3 answers

ValueError: nlp.add_pipe now takes the string name of the registered component factory, not a callable component

The following link shows how to add custom entity rule where the entities span more than one token. The code to do that is below: import spacy from spacy.pipeline import EntityRuler nlp = spacy.load('en_core_web_sm', parse=True, tag=True,…
Learner
  • 592
  • 1
  • 12
  • 27
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
4 answers

mockito -using one of the values from list of values to compare in matcher

My method interface is Boolean isAuthenticated(String User) I want to compare from list of values if any of the users are passed in the function from the list, then it should return…
Amol Aggarwal
  • 2,674
  • 3
  • 24
  • 32
7
votes
1 answer

ScalaTest assert and matchers

I used ScalaTest in my Scala Play project. But I have a question here, when to use normal assert(xxx === yyy) and when to use ScalaTest matchers like xxx should be yyy. Personally I prefer to use assert as it is simple and clean. Also can take…
ttt
  • 3,934
  • 8
  • 46
  • 85
7
votes
3 answers

pattern.compile help java program

I have written a program to parse a text file which contains a sample C program with if, else and while condition. I have 2 ArrayLists and my program will parse through the file. I'm using Matcher and have specified pattern Strings in…
Polynomial Proton
  • 5,020
  • 20
  • 37
7
votes
0 answers

Why does scalatest matcher `should not include` not compile with FlatSpec, but work with FunSuite

I am using scala 2.9.3 and scalatest_2.9.3-1.9.2. I am having trouble using the matchers to assert that a string does NOT contain a substring: import org.scalatest.FlatSpec import org.scalatest.matchers.ShouldMatchers._ class Learning extends…
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
7
votes
2 answers

ScalaTest - writing custom matchers

I am running into a problem while writing a custom matcher for NodeSeq: private def matchXML(expected: NodeSeq) = new Matcher[NodeSeq] { def apply(left: NodeSeq): MatchResult = MatchResult(left xml_== expected, "XML structure was not the same…
Bober02
  • 15,034
  • 31
  • 92
  • 178
6
votes
1 answer

Ignore case class field in ScalaTest matching

Assume I have case class with several field members: case class User(name: String, ..., createdAt: LocalDateTime) How could I check equality without taking into account createdAt field? Is there a better way than: val expected = User("username",…
6
votes
1 answer

Is there any proper matcher to parse and compare LocalDateTime field in Json response from MockMvc

I'm testing get method of my SpringBoot controller, which is providing objects, written in base in particular time range. I realize that I can get json after mockMvc performing and parse it with object mapper, use some streams and one assert, but I…
Ludov Dmitrii
  • 425
  • 5
  • 9
6
votes
2 answers

Mockito Unit Test Case call is Ambiguous (Need to get it to not be ambiguous)

How should I write the following Mockito Matchers so that the call is not ambiguous? The actual function call I am trying to mock in my code is: //Variables String url = http://theServer:8080/oath2-v1/token; HttpEntity request = new…
darmbre
  • 116
  • 1
  • 6
6
votes
2 answers

Mockito matcher that compares items in Set, not the Set reference itself

when(validator.isValid(Sets.newHashSet("valid"))).thenReturn(true); When validator.isValid(set) is invoked it returns false. It is because the validator implementation creates a new set that is different that the passed one (reference is different)…
Felix
  • 3,999
  • 3
  • 42
  • 66
6
votes
1 answer

How to write mockito matcher for byte[]?

I need a complex Matcher for byte[]. The code below does not compile since argThat returns Byte[]. Is there a way to write a dedicated Matcher for an array of primitive types? verify(communicator).post(Matchers.argThat(new…
BetaRide
  • 16,207
  • 29
  • 99
  • 177
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