I start using gmock for my code recently, some of which rely on some other outside services or interfaces, so gmock is kind of perfect for me.
Say I have a trivial class A to mock
class A
{
void foo(int& bar) {
// will change bar
bar = GetingResultFromSomeOutsideService();
}
};
Now I create a mock class
class MockA: public A
{
MOCK_METHOD1(foo, void(int& bar));
};
In my test case, I try to mock the behavior of A::foo of changing bar with ON_CALL, but I don't know how exactly.
For example, I want my MockA::foo always set bar to 1, so that my other codes invoking A::foo will always get bar == 1 like this:
// some other piece of codes invoking A::foo
int bar = 0;
A my_a;
my_a.foo(bar);
cout << bar; // always output "1" with gmock
Any hint?
Related
I have a test suite in gtest.My Test Fixture class has two tests,one is Test_F(functional),Test_F(performance).
I have the implementation in such a way that Test_F(performance) is dependent on the run of Test_F(functional).
I want to handle the case,where when Test_F(functional) is disabled.So,my approach is calling Test_F(functional) from inside Test_F(performance).
I am not sure,how this is done in gtest,calling a test_function,inside another test_function.
Any help is appreciated.
AFAIK there's no option to call test function inside another test function. However, to share the code between test cases, you can define a helper method in the test suite:
class FooBarTestSuite : public ::testing::Test {
public:
TestSuite() {
// init code goes here
// foo and bar are accessible here
}
~TestSuite() {
// deinit code goes here
// foo and bar are accessible here
}
void CommonCode() {
// common test case code here
// foo and bar are accessible here
}
ClassFoo foo{};
ClassBar bar{};
};
TEST_F(FooBarTestSuite, PerformanceTest) {
CommonCode();
// performance-specific code here
}
TEST_F(FooBarTestSuite, FunctionalTest) {
CommonCode();
// functional-specific code here
}
In order to run a specific test case, use --gtest_filter=*Performace* or --gtest_filter=*Functional* as the parameter.
In this example bellow I will obtain the following behavior:
EXPECT_CALL(barMock, doBar(7))...
Expected arg #0: is equal to 7
Actual: 5
Expected: to be called once
Actual: called once - saturated and active
#include <gmock/gmock.h>
#include <gtest/gtest.h>
class IBar
{
public:
virtual bool doBar(int barParam) = 0;
};
class BarMock : public IBar
{
public:
MOCK_METHOD1(doBar, bool(int));
};
class Foo
{
public:
Foo(IBar& bar_)
: bar{ &bar_ }
{
}
void doFoo()
{
bar->doBar(7);
bar->doBar(5);
}
IBar* bar;
};
class FooBarTest : public ::testing::Test
{
public:
void SetUp() override
{
ON_CALL(barMock, doBar(testing::_)).WillByDefault(testing::Return(true));
}
testing::NiceMock<BarMock> barMock;
};
TEST_F(FooBarTest, OnCallExpectCallSameMethod)
{
Foo foo(barMock);
ON_CALL(barMock, doBar(5)).WillByDefault(testing::Return(true));
EXPECT_CALL(barMock, doBar(7)).WillOnce(testing::Return(true));
foo.doFoo();
}
Google test versions tested:
1.8.0
1.8.1
Is this intended to cause an error?
How does google test sets the
order calls? Does EXPECT_CALL has higher priority only in this
scenario?
Yes, it is intended to be an error. One of the GoogleMock policies is that in case of possible ambiguity it's better to throw an error an make user state his intention explicitly.
Adding EXPECT_CALL macro effectively says "I care about calls of this method". Whenever EXPECT_CALL is set, GoogleMock will try to match every EXPECT_CALL it has seen in reverse order of declaration (so the latest defined expects are matched first).
One of the reasons for this design is to allow overriding less specific expects with more specific ones (e.g. in constructor of test fixture you set less restrictive expects, but for certain tests you want to match more precisely). Documentation.
However, there is a way of "ignoring" already fulfilled expectations by adding .RetiresOnSaturation()
TEST_F(FooBarTest, OnCallExpectCallSameMethod)
{
Foo foo(barMock);
ON_CALL(barMock, doBar(5)).WillByDefault(testing::Return(true));
EXPECT_CALL(barMock, doBar(7))
.WillOnce(testing::Return(true))
.RetiresOnSaturation();
foo.doFoo();
}
RetiresOnSaturation() will make expectations retired after it has been saturated (i.e. called as many time as expected). GoogleMock will skip over retired expectations as long as there are still any non-retired ones (if all of them are retired then it still prints an error).
If you however need to accept calls doBar(5) before doBar(7), then the only way to do so is to define it as expectation as well:
TEST_F(FooBarTest, OnCallExpectCallSameMethod)
{
Foo foo(barMock);
EXPECT_CALL(barMock, doBar(5)).WillRepeatedly(testing::Return(true));
EXPECT_CALL(barMock, doBar(7))
.WillOnce(testing::Return(true))
.RetiresOnSaturation();
foo.doFoo();
}
RetiresOnSaturation() is still needed because of LIFO (last in, first out) processing of expectations.
I'm attempting to write Mocks for Private / Non Virtual / Static functions and come across a way to do the same.
Here is how it looks like..
Lets assume that I have a class A which needs to be mocked and used inside class UsingA. The definition of both classes looks like
class A
{
friend class UsingA;
int privateFn() {}
public:
int nonVirtual() {}
};
// The UsingA class
class UsingA {
A &a1;
public:
UsingA(A & _a1) : a1(_a1) {}
int CallFn() {
return a1.nonVirtual();
}
int CallFn2() {
return a1.privateFn();
}
};
I know that Mocks are meant for generating the behavior of the class and while creating Mocks, we need to derive from the original class.
However, to Mock the behavior I decided not to derive from the original class, instead comment the class A and generate a Mock class with the same Name i.e class A.
Here is how my mock class looks like
// Original class A is commented / header file removed
class A {
public:
MOCK_METHOD0(nonVirtual, int());
MOCK_METHOD0(privateFn, int());
};
And my tests are usual mock tests
TEST(MyMockTest, NonVirtualTest) {
A mstat;
UsingA ua(mstat);
EXPECT_CALL(mstat, nonVirtual())
.Times(1)
.WillOnce(Return(100));
int retVal = ua.CallFn();
EXPECT_EQ(retVal,100);
}
TEST(MyMockTest, PrivateTest) {
A mstat;
UsingA ua(mstat);
EXPECT_CALL(mstat, privateFn())
.Times(1)
.WillOnce(Return(100));
int retVal = ua.CallFn2();
EXPECT_EQ(retVal,100);
}
And everything works fine and I'm able to test UsingA by this mock.
Question is.
This looks easier and serves the purpose, still I haven't seen this kind of examples while browsing for google mock examples. Is there anything that would go wrong if I do this?
Honestly, I didn't find any.
NOTE: Folks, I'm using friend for demonstration only. My actual use case is totally different. Thanks
The wrong is that you are not testing real code, because of that:
comment the class A
generate a Mock class with the same name
These operations alter the code under test.
An example what can go wrong:
Change return type: long nonVirtual in Mock - previously was int
Test that on, let say, nonVirtual() == 0xFF'FFFF'FFFF (which is bigger than INTMAX) some action is being done
Forget to change in real A - so real UsingA have branch that is tested but never reachable in real code
An example code:
class A {
public:
MOCK_METHOD0(nonVirtual, long()); // change
MOCK_METHOD0(privateFn, int());
};
void UsingA::processA()
{
if (a.nonVirtual() > VERY_BIG_NUMBER)
{
throw runtime_error("oops");
}
}
TEST_F(UsingATest, throwOnVeryBigNumber)
{
EXPECT_CALL(aMock, nonVirtual()).WillOnce(Return(VERY_BIG_NUMBER + 1));
ASSERT_THROW(objectUndertTest.processA());
}
But real A did not change - so we test non reachable code in UsingA class:
class A {
public:
int nonVirtual(); // not changed
...
};
The best solution is (in order):
To test in isolation you have to isolate classes - so to use dependency injection (virtual functions etc, base interfaces, etc...) - this is sometimes called London School of TDD
Test both classes A and UsingA w/o any stubbing - test them together in one testcase - thus you test real code - this is called Detroit Shool of TDD
Separate by template code with good restriction on interface - this approach is most similar to yours:
Regarding 3 - you might use something like this:
template <class T = A>
class UsingA {
T &a1;
public:
UsingA(T & _a1) : a1(_a1) {}
long CallFn() {
using ANonVirtualResult = std::invoke_result_t<&T::nonVirtual>;
static_assert(std::is_same<long, ANonVirtualResult>::value);
return a1.nonVirtual();
}
...
};
And in test:
class UsingATest : public ::testing::Test
{
protected:
StrictMock<AMock> aMock;
using ClassUnderTest = UsingA<AMock>;
ClassUnderTest objectUnderTest{aMock};
};
TEST_F(UsingATest, useNonVirtual)
{
const auto VALUE = 123456;
EXPECT_CALL(aMock, nonVirtual()).WillOnce(Return(VALUE));
ASSERT_EQ(VALUE, objectUnderTest.CallFn());
}
You might note that some assumption about A might be tested during compilation as static_assert or via some SFINAE technics (more complicated).
Actually, there are examples with template code in googlemock as workaround for mocking classes w/o virtual functions.
We use your type of using mocks inside a few of our test projects to check callbacks on a larger class that we pass along using dependency injection. In our case, the methods are declared virtual.
In your case, they are not. Your mock implementation would hide the original implementation - if there was any. So I don't think there's an issue here.
I have a program Foo that runs on a device and first calls method1() and then method2() on an external module Bar.
When both are called, then in response, Bar calls request() on Foo.
To mock Bar, it has a reference to Foo and a method call_Request() which calls foo.request()
// in class BarTest
#Test
public void barFlow() {
InOrder inOrder = inOrder(mockBar);
inOrder.verify(mockBar).method1(anyInt());
inOrder.verify(mockBar).method2();
// what to put as 'thenCallMethod'?
when(mockBar.method2()).thenCallMethod().call_Request();
}
...
// in class Bar
public void call_Request() {
foo.request();
}
I cannot simply put a call to call_Request() in method2 because only when both methods are called in order does the response happen. Other times, different responses may happen.
what Mockito trick will do 'thenCallMethod'?
(I am using the Mockito ver 1.10.19 bundled with Android Studio 2.1)
You seem to have no clear definition of what your unit under test is. There seem to be 3 possiblities:
there are separate tests for Foo and Bar and the unit under test is an instance of Foo using a mock of Bar and an instance of Bar using a mock of Foo
the unit under test is an instance of Foo using a real instance of Bar, but the Bar instance uses a Foo mock
the unit under test is an instance of Foo using a real instance of Bar which in turn uses the existing instance of Foo
In case 1. you could test the following for Bar:
#Test
public void testBar1() {
Foo mockFoo = Mockito.mock(Foo.class);
Bar bar = //obtain unit under test that uses mockFoo
bar.method1(1/*or whatever int value is appropriate*/);
bar.method2();
Mockito.verify(mockFoo).request();
}
#Test
public void testBar1() {
Foo mockFoo = Mockito.mock(Foo.class);
Bar bar = //obtain unit under test that uses mockFoo
bar.method2();
bar.method1(1/*or whatever int value is appropriate*/);
Mockito.verify(mockFoo, Mockito.never()).request();
}
And the test for Foo would look like:
Bar mockBar = Mockito.mock(Bar.class);
Foo foo = //obtain unit under test that uses mockBar
// invoke method on foo that would trigger calls to mockBar
InOrder inOrder = inOrder(mockBar);
inOrder.verify(mockBar).method1(anyInt());
inOrder.verify(mockBar).method2();
In case 2. you would basically repeat the test for Foo from case 1. But you would just verify that Foo.request() was invoked on the mock of Foo used by the instance of Bar. The order of invocations on the Bar instance are not of any interest for this test case.
In case 3. you wouldn't be able to verify any invocations, because there are no mocks used at all.
Using something like
when(mockBar.method2()).thenCallMethod().call_Request();
doesn't make much sense, because you give a mock an implementation, that it might not have in production code.
Is it possible to scope an expectation using Google Mock? In other words, let's say I have the following test fixture:
class Fixture : public testing::Test
{
public:
void SetUp();
void TearDown();
ObjectUnderTest testObject;
MockObject mock;
};
Now, in the SetUp() function, I want to allow a mock function call as many times as is necessary during initialisation:
void Fixture::SetUp()
{
EXPECT_CALL(mock.DoStuff(_)).Times(Any());
testObject.Initialise(mock);
}
After this, I want this particular expectation to go out-of-scope and any calls to DoStuff() to generate a failure. I can't use RetiresOnSaturation() because it will never saturate!
In other words, if I have the following test case:
TEST_F(Fixture, DoesWhatItsSupposedTo)
{
EXPECT_CALL(mock, DoStuff(Eq(3)));
testObject.DoSomething(mock);
}
I would like this to fail if DoSomething() calls DoStuff(4) on the mock object. With gmock default behaviour (which is right for most scenarios), it will first check the second expectation that won't match; it will then check the expectation in SetUp() that will match and pass.
Does anyone know if this is possible?
It may not be really answer to your question, but you can explicitly forbid a call using .Times(0):
TEST_F(Fixture, DoesWhatItsSupposedTo)
{
EXPECT_CALL(mock, DoStuff(Eq(3)));
EXPECT_CALL(mock, DoStuff(Ne(3)))
.Times(0);
testObject.DoSomething(mock);
}
This will expect one call of DoStuff(3)and no other calls of DoStuff() within this test - any call is first checked against DoStuff(Ne(3)). Before the test, any call will still be accepted.