I am trying to make Mockito validate a call to library function which code cannot be changed else would have changed signature of varargs to publish(Record record, Record... records)
publish(Record record)
publish(Record... records)
To validate execution of second function, using following code
verify(publisher).publish(ArgumentMatchers.<Record>any());
However above call always keep try to check for publish call with NonArgs function and fails. Any suggestions how to make Mockito to check for var args function.
Used mockito Version for this test is 3.11.2
You need to pick a right overload when you call the verify method.
Mockito.verify(publisher).publish(ArgumentMatchers.<MyRecord[]>any());
By specifying ArgumentMatchers.<MyRecord>any() you picked the wrong overload .
Notes:
any() implements VarargMatcher - a special marker interface meaning that this matcher can be used mo match varargs
each vararg is matched by VarargMatcher separately, in a for loop
Other notes:
Record is a keyword in modern Java, you might consider renaming your class to sth different
Related
I work in Mocha. This is ONE of the cases I need mocks/stubs for, but not the only one.
I have a one big serviceHelper file and I'm writing unit tests for it.
I have a few functions, let's say verifyAdmin(), verifyUser() etc.
There's one function that collects them together with a switch statement - if param is 'admin' it calls verifyAdmin, if param is 'user' calls verifyUser etc.
So since I have the first two unit tested, there's no point in re-testing them when trying to test the one function that calls them in the switch statement. I want to mock them and just make sure the proper functions were called.
//serviceHelper.js
async function verifyAdmin(){
does things and returns isAdmin;
}
async function verifyUser(){
does things and returns isUser;
}
async function checkSession(session){
switch statement that calls either one of these
}
The issue is - from what I'm finding - there's no way to mock/stub them if they're in the same file.
Or maybe is there and I just can't find it?
Or maybe I should split these in 2 separate files? Is that a good approach? Kind of like 'verificationHelper' module? But then the main verification function (the one with the switch statement) would still be in the service helper, which might be confusing... What's my solution here?
I am currently trying to develop test-driven with Flutter and Dart.
I have an object that has two methods of which the first one does an http call and the second one calls the first method.
In order to test the first function I mock the dependencies of the that function (namely the http call).
Now I want to test the second method, but I was not able to find a way to mock only the first function while keeping the rest of the object intact. As a result I can only mock the dependencies of the first method again which results in the entire function being executed all over. This goes against the whole purpose of unit testing.
It seems like there is only an all or nothing approach when it comes to mocking objects. I wonder how one is to go about a case where some object is reliant on methods on the same object.
Using Fake and Mock does not allow me to call the original method. spy is deprecated and assigning a mock function to one of the functions does not work since Dart does not allow me to reassign a method.
I found a way to solve my problem. According to this link mentioned in the comments "[t]esting with real objects is preferred over testing with mocks". In my case I just subclassed the main object I was testing and replaced the (second-)method that I was not testing. This seems to do the trick.
Is there any way to test a function call via GoogleTest for c++ without creating mock object, e.g. we have the following production code:
if (a)
method(x);
I would like to test whether the method will be called in the case a is True and a is False. I would like to construct a test that does exactly the same what Google Test's EXPECT_CALL does, but EXPECT_CALL works only with the mock object's method. In my case I would prefer not to use a mock (there is no need to create any object).
As state here,
It's possible to use Google Mock to mock a free function (i.e. a C-style function or a static method). You just need to rewrite your code to use an interface (abstract class).
Their "It's possible" is misleading, as you have to rewrite code to use class (abstract, or provided by template), and so no longer use free functions.
If you are trying to fake a free function, you may want to look into the Fake Function Framework (fff). It allows you to replace free functions with fake implementations that can be used in a similar way to GoogleMock.
It works seamlessly with GoogleMock and GoogleTest.
Database.Setup(x => x.Update(It.IsAny<Subscription>()))
.Callback<object>(sub => SavedSubscription = sub as Subscription);
This line works with PetaPoco. Because of project requirements, had to switch to Dapper (and also had to add DapperExtensions in order to have CRUD methods) so now I am getting this error:
An expression tree may not contain a call or invocation that uses
optional arguments
Any ideas how to handle this?
You can't use Moq to mock methods that have optional parameters (as Update does). See this question for more details, though you're stuck specifying all parameters to the Update method.
Note that this is a limitation inherent to the framework and not Moq; you can't pass a method with a default parameter to an Expression. Jon Skeet gives a good demonstration why here.
I need to verify I method call with a specific parameters
How can I do?
Im work in java with mockito, and use junit.
You can simply provide the parameters you require in the verify statement, assuming that the classes for those parameters have the equals method properly defined.
verify(myMock).myMethodCall(someParmValue1, someParmValue2);
If equals is not the criteria you wish to use, you can use Matchers on the arguments, but note that if you use a Matcher for any argument then you have to use matchers for all arguments. So as an example if you want to ensure that the arguments in your verify were actually the same instances as (object identity instead of equality), you could use the Matchers.same() Matcher:
verify(myMock).myMethodCall( same(someParmValue1), same(someParmValue2));