How to expect void method call with any argument using EasyMock - unit-testing

As a part of unit test I need to mock a void function(Which accept any non-primitive paramter. e.g. MAP) call with any argument.
mockObj.myMethod(<anyObject>)
Is it possible to do this with EasyMock?

Use either of the anyObject methods: anyObject() or anyObject(T)
So
expect(mockObj.myMethod(anyObject()));
See the Flexible Expectations with Argument Matchers section of the documentation

Related

GMock EXPECT_CALL of an overloaded function with abstract argument type [duplicate]

I use Gtest to verify my C++ code, and now I face a mocking problem.
For several reasons, I have some methods, which have the same name, but different type arguments and implementation, like
void foo(int& i);
void foo(double& d);
void foo(float& f);
For them, I make mock methods, like
MOCK_METHOD1(foo, void(int&));
MOCK_METHOD1(foo, void(double&));
MOCK_METHOD1(foo, void(float&));
However, I could not use EXPECT_CALL for them. In a test code, I set action for foo(int), like
EXPECT_CALL(mock_object, foo(_)).WillOnce(DoAll(SetArgReferee<0>(10),Return()));
but, compiler failed because target is ambiguous among int, double and float.
Is there any way to use EXPECT_CALL for specific type mock method?
I failed to use testing::MatcherCast and testing::SafeMatcherCast, because they only accept const type. However, I need to update the argument, so I could not use const.
You can use typed wildcard (documentation):
EXPECT_CALL(mock_object, foo(An<int&>())).WillOnce(SetArgReferee<0>(10));
EXPECT_CALL(mock_object, foo(A<double&>())).WillOnce(SetArgReferee<0>(10.));
A<> and An<> mean exactly the same, they have two names just for nicer reading.
See it online on godbolt
Side note: You don't have to Return() from void method.

EXPECT_CALL without mock in Google Test

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.

How to create custom matchers in Mockito?

I am using Mockito for unit testing. And there are many matchers like anyString(), anyBoolean() in Mockito. But suppose if I have a custom enum like
Enum LoginType.java
//LoginType.java
public enum LoginType {
FACEBOOK,
EMAIL,
GOOGLE
}
In one of the method arguments I need to pass an instance of LoginType. How do I pass the argument without explicitly passing LoginType.FACEBOOK or LoginType.GOOGLE. Something like anyString(). Any hint in that direction will be useful.
For any behavior, just calling Matchers.any() may be good enough on Java 8. That's when parameter type inference came out.
You might also choose Matchers.any(LoginType.class), which has pure any() behavior in Mockito 1.x but will provide type checking in Mockito 2.0 and beyond. In either case, passing in the class literal will help Java get the type information you need for inference.
For related problems:
If you have a generic type, the class literal isn`t enough either; you need to specify it as an explicit method parameter:
Matchers.<YourContainer<YourType>>any();
...or extract to a static helper method, which you need to do instead of a constant or local variable because Mockito matchers work via side effects:
public static LoginType anyLoginType() {
return Matchers.any();
}
Finally, for future readers who might be here to implement custom matching logic, look for Matchers.argThat or MockitoHamcrest.argThat to adapt a Hamcrest-style Matcher object into a Mockito method call.

How do I verify a method call and ignore the return value in EasyMock

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));

Hippomocks: How to expect only some of the arguments passed to ExpectCall()?

In the mocking framework Hippomocks it is possible to check the passed arguments within an expectation. For example:
void Class::Method(int arg1);
...
m_mockRepository_p->ExpectCall(someObject_p, Class::Method).With(1);
Is there a possibility to check only some of the passed arguments? What should I write instead of ??? in the following code?
void Class::Method(int arg1, char* buffer_p);
...
m_mockRepository_p->ExpectCall(someObject_p, Class::Method).With(1,???);
For your information: The use case is that someObjet_p creates internally a buffer to read data in it and passes it to Class::Method. Firstly I don't care in which buffer someObject_p would like the data to put in. Secondly I don't have access to the internals of someObject_p to pass it to With() - which is quite reasonable.
Note: I've tagged the question as C++ as I make use of ExpectCall but I'd guess the same would apply to ExpectCallFunc for flat functions.
Second note: Hippomocks provides methods for expectations named "Match()", "getArgs()", "assignArgs()" and "matchesArgs()" but neither did I find any documentation on it nor do I know whether they are intended to be used (for my use case) or not.
Pass in a Don't-Care as that parameter:
m_mockRepository_p->ExpectCall(someObject_p, Class::Method).With(1, _);