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.
matcher
465
questions
208
votes
6
answers
423k
views
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<? extends A> a) {}
}
How can I always return a new B() regardless of which class is ...
205
votes
4
answers
208k
views
Mockito: List Matchers with generics
Mockito offers:
when(mock.process(Matchers.any(List.class)));
How to avoid warning if process takes a List<Bar> instead?
133
votes
4
answers
68k
views
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 ...
94
votes
4
answers
18k
views
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 ...
87
votes
3
answers
46k
views
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
...
71
votes
2
answers
68k
views
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 ...
69
votes
4
answers
47k
views
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 ...
69
votes
2
answers
101k
views
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 ...
55
votes
3
answers
132k
views
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
...
55
votes
9
answers
80k
views
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<Integer> actual = new ArrayList<...
48
votes
2
answers
24k
views
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 ...
48
votes
1
answer
20k
views
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:
...
25
votes
2
answers
11k
views
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 ...
23
votes
5
answers
19k
views
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 = ...
23
votes
4
answers
55k
views
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 ...
22
votes
3
answers
26k
views
Jest Equality Matcher For Strings That Disregards Whitespace
Jest's toEqual matcher takes whitespace into account when checking for equality. When formatting the expected value in tests it is impossible to do so in a way that matches a string containing ...
21
votes
6
answers
38k
views
Jasmine toEqual for complex objects (mixed with functions)
Currently, I have a function that sometimes return an object with some functions inside. When using expect(...).toEqual({...}) it doesn't seem to match those complex objects. Objects having functions ...
20
votes
7
answers
11k
views
Rspec view testing with capybara and rails3
I really like the way RSpec is able to separate controller and view tests but have some problems with getting capybara matchers to work in a view test. What i basically try to achieve is sth like this:...
20
votes
3
answers
15k
views
Is there a Hamcrest matcher to check that a Collection is neither empty nor null?
Is there a Hamcrest matcher which checks that the argument is neither an empty Collection nor null?
I guess I could always use
both(notNullValue()).and(not(hasSize(0))
but I was wondering whether ...
18
votes
2
answers
44k
views
Replace HTML codes with equivalent characters in Java [duplicate]
Currently I'm working on converting HTML codes with equivalent characters in java.
I need to convert the below code to characters.
è - è
® - ®
& - &
ñ ...
18
votes
4
answers
18k
views
How to do `cy.notContains(text)` in cypress? [duplicate]
I can check if text exists in cypress with cy.contains('hello'), but now I delete hello from the page, I want to check hello doesn't exist, how do I do something like cy.notContains('hello')?
17
votes
3
answers
17k
views
Does an RSpec2 matcher for matching Hashes exist?
Note to future readers: think RSpec does not consider your Hashes equal? One might be an OrderedHash, but from the regular RSpec output you can't tell. This was the problem that prompted this post.
...
17
votes
6
answers
11k
views
During suite tests EasyMock says 0 matchers expected 1 recorded
So I've been using EasyMock's class extension for a while now. All of a sudden I'm getting this exception, but only when I run the entire test suite:
java.lang.IllegalStateException: 0 matchers ...
17
votes
1
answer
8k
views
rspec The expect syntax does not support operator matchers, so you must pass a matcher to `#to`
using the new expect syntax:
expect(@line.filter_results_and_display_them).to == @processed
Getting this error:
ArgumentError: The expect syntax does not support operator matchers, so you must ...
16
votes
2
answers
21k
views
Mockito: Match any String except one [duplicate]
How can I write a matcher using Mockito that matches any string except a specific one?
I have tried using some hamcrest matchers to negate and combine other matchers, but the hamcrest matchers all ...
15
votes
2
answers
4k
views
How to avoid double-extracting of overlapping patterns in SpaCy with Matcher?
I need to extract item combination from 2 lists by means of python Spacy Matcher. The problem is following:
Let us have 2 lists:
colors=['red','bright red','black','brown','dark brown']
animals=['fox',...
15
votes
1
answer
12k
views
Mockito Matchers.any(...) on one argument only
I want to do this:
verify(function, Mockito.times(1)).doSomething(argument1, Matchers.any(Argument2.class));
Where argument1 is a specfic instance of type Argument1 and argument2 is any instance of ...
15
votes
2
answers
13k
views
Is It Possible To Extend A Jest / Expect Matcher
I would like to extend Jest's isEqual matcher so that the expected value is transformed before comparison (this allows me to use multiline strings in tests). All I need to do is run the expected value ...
15
votes
5
answers
7k
views
Junit Matcher for comparators?
For several days I am using now Junit's Matchers feature. Everything is working OK but I am looking for a matcher which uses a comparator for comparing and which does not rely on the objects equals ...
15
votes
2
answers
24k
views
Mockito Matchers: matching a Class type in parameter list
I am working with Java, Spring's RestTemplate, and Mockito, using Eclipse. I am trying to mock Spring's rest template, and the last parameter for the method I am mocking is a Class type. Below is ...
14
votes
8
answers
31k
views
Jest: expect object not to have property
I want to write a test that asserts a given object does not have certain properties.
Say I have a function
function removeFooAndBar(input) {
delete input.foo;
delete input.bar;
return input;
}
...
14
votes
2
answers
6k
views
How to stub a method call with an implicit matcher in Mockito and Scala
My application code uses AService
trait AService {
def registerNewUser (username: String)(implicit tenant: Tenant): Future[Response]
}
to register a new user. Class Tenant is a simple case class:...
11
votes
2
answers
14k
views
Matching multiple properties in one Matcher
I need to write Matcher which will check multiple properties. For single property i've used:
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import org....
11
votes
2
answers
36k
views
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
My method in the service and test class :
public void updateSubModuleOrder(Long[] data, Long moduleSysId, Long userId) {
try {
for (int i = 0; i < data.length; i++) {
...
11
votes
1
answer
3k
views
How to show custom failure message in Specs2 (Scala)?
For example, for code like this:
myNum must beEqualTo("SOME INTERESTING TEXT")
The message will be like the following:
java.lang.Exception: ArrayBuffer() doesn't have size 1 but size 0
Is there an ...
11
votes
1
answer
3k
views
Is there any way to use Jasmine default matchers within custom matchers?
I have a custom matcher in some Jasmine test specs of the form:
this.addMatchers({
checkContains: function(elem){
var found = false;
$.each( this.actual, function( actualItem ){
...
10
votes
5
answers
8k
views
Problem with using spacy.matcher.matcher.Matcher.add() method
I am getting an error when trying to use spacy matcher:
~\Anaconda3\lib\site-packages\spacy\matcher\matcher.pyx in spacy.matcher.matcher.Matcher.add()
TypeError: add() takes exactly 2 positional ...
10
votes
2
answers
10k
views
How to use the isA-Matcher
I have a certain method that delivers a Restriction-object (where Restriction is an interface). And since its implementation is already testet, I just want to test if my method actually delivers a ...
9
votes
2
answers
3k
views
Random RSpec failures comparing Floats (Eq matcher)
I'm not even sure where to begin here. Sorry if this is a duplicate, but I don't even know what to search for or what this particular issue is called.
Randomly, and not all that often, a test in my ...
9
votes
2
answers
3k
views
In spacy, Is it possible to get the corresponding rule id in a match of matches
In Spacy 2.x, I use the matcher to find specific tokens in my text corpus. Each rule has an ID ('class-1_0' for example). During parse, I use the callback on_match to handle each match. Is there a ...
9
votes
1
answer
17k
views
Mock a method with an object parameter with Mockito
In my unit test i want to mock the interaction with elasticsearch by doing the following
when(cityDefinitionRepository.findCitiesNearby(geoPoint, SOURCE, 2)).thenReturn(cityDefinitionsArrival);
when(...
8
votes
3
answers
21k
views
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('...
8
votes
1
answer
16k
views
Mockito matcher to match a method with generics and a supplier
I'm using Java 1.8.0_131, Mockito 2.8.47 and PowerMock 1.7.0. My question is not related to PowerMock, it is released to a Mockito.when(…) matcher.
I need a solution to mock this method which is ...
8
votes
2
answers
9k
views
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 ...
8
votes
1
answer
8k
views
How to make ExampleMatcher to match just one property?
How to implement ExampleMatcher, to match just one property randomly from my class and ignore the other properties?
Assume my class like this :
Public Class Teacher() {
String id;
String name;
...
8
votes
2
answers
4k
views
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 ...
8
votes
1
answer
2k
views
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 ...
8
votes
1
answer
371
views
Using clang matchers to detect sequence of patterns
Is it possible to use clang matchers to identify sequence of patterns in a program?
For example I need to find cases in which pattern1 happens before pattern2.
For instance:
Pattern1 = assigning a ...
8
votes
1
answer
5k
views
OpenCV Finding Correct Threshold to Determine Image Match or Not with Matching Score
I'm currently making a recognition program using various feature extractor and various matcher. Using the score from the matcher, I want to create a score threshold which can further determine if it's ...
7
votes
4
answers
6k
views
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 true.
when(...