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

Mockito match any class argument

Is there a way to match any class argument of the below sample routine? class A { public B method(Class a) {} } How can I always return a new B() regardless of which class is passed into method? The following attempt only works…
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
203
votes
4 answers

Mockito: List Matchers with generics

Mockito offers: when(mock.process(Matchers.any(List.class))); How to avoid warning if process takes a List instead?
Philippe Blayo
  • 10,610
  • 14
  • 48
  • 65
128
votes
4 answers

RSpec: Expect to change multiple

I want to check for many changes in a model when submitting a form in a feature spec. For example, I want to make sure that the user name was changed from X to Y, and that the encrypted password was changed by any value. I know there are some…
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
93
votes
3 answers

How to show custom failure messages in ScalaTest?

Does anyone know how to show a custom failure message in ScalaTest? For example: NumberOfElements() should equal (5) Shows the following message when it fails: 10 did not equal 5 But i want more descriptive message like: NumberOfElements should…
Udayakumar Rayala
  • 2,264
  • 1
  • 20
  • 17
86
votes
3 answers

Multiple correct results with Hamcrest (is there an or-matcher?)

I am relatively new to matchers. I am toying around with hamcrest in combination with JUnit and I kinda like it. Is there a way, to state that one of multiple choices is correct? Something like assertThat( result, is( either( 1, or( 2, or( 3 ) ) ) )…
Mo.
  • 15,033
  • 14
  • 47
  • 57
70
votes
2 answers

When to use ** (double star) in glob syntax within JAVA

Directly from this Java Oracle tutorial: Two asterisks, **, works like * but crosses directory boundaries. This syntax is generally used for matching complete paths. Could anybody do a real example out of it? What do they mean with "crosses…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
68
votes
4 answers

How to write a matcher that is not equal to something

I am trying to create a mock for a call. Say I have this method I am trying to stub out: class ClassA { public String getString(String a) { return a + "hey"; } } What I am trying to mock out is: 1st instance…
Churk
  • 4,556
  • 5
  • 22
  • 37
66
votes
2 answers

What's the difference between Mockito Matchers isA, any, eq, and same?

I am confused on what's the difference between them, and which one to choose in which case. Some difference might be obvious, like any and eq, but I'm including them all just to be sure. I wonder about their differences because I came across this…
Silly Sally
  • 1,027
  • 2
  • 10
  • 15
55
votes
9 answers

Why doesn't this code attempting to use Hamcrest's hasItems compile?

Why does this not compile, oh, what to do? import static org.junit.Assert.assertThat; import static org.junit.matchers.JUnitMatchers.hasItems; ArrayList actual = new ArrayList(); ArrayList expected = new…
ripper234
  • 222,824
  • 274
  • 634
  • 905
52
votes
3 answers

Spring Security - Authorize Request for certain URL & HTTP-Method using HttpSecurity

Is there any way to authorize a POST http-request to a specific URL using org.springframework.security.config.annotation.web.builders.HttpSecurity ? I'm using HttpSecurity as: @Override protected void configure(HttpSecurity http) throws…
Ignasi
  • 5,887
  • 7
  • 45
  • 81
47
votes
2 answers

How to test not equal with matcher in flutter

I'm doing testing on render objects in Flutter. I'd like to check for inequality like this (simplified): testWidgets('render object heights not equal', (WidgetTester tester) async { final renderObjectOneHeight = 10; final renderObjectTwoHeight…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
44
votes
1 answer

How to get a Jest custom matcher working in typescript?

I regularly have unit tests where I need to compare two moment objects. I'd us moment's built-in function moment.isSame(moment) to compare them. However, this means my assertion will look like this: expect(moment1.isSame(moment2)).toBeTrue(); I…
Martao
  • 795
  • 2
  • 6
  • 12
25
votes
2 answers

String includes many substrings in ScalaTest Matchers

I need to check that one string contains many substrings. The following works string should include ("seven") string should include ("eight") string should include ("nine") but it takes three almost duplicated lines. I'm looking for something…
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
23
votes
4 answers

EasyMock : java.lang.IllegalStateException: 1 matchers expected, 2 recorded

I am having a problem with EasyMock 2.5.2 and JUnit 4.8.2 (running through Eclipse). I have read all the similar posts here but have not found an answer. I have a class containing two tests which test the same method. I am using matchers. Each test…
Anne
  • 233
  • 1
  • 2
  • 4
23
votes
5 answers

Is there any Hamcrest Matcher for java.util.Optional?

I am looking for a Hamcrest Matcher to unit test methods that return a java.util.Optional type. Something like: @Test public void get__Null(){ Optional element = Element.get(null); assertThat( sasi ,…
borjab
  • 11,149
  • 6
  • 71
  • 98
1
2 3
30 31