How to only throw exception when the mocked method is called for the first time? - unit-testing

I have a method of a mocked object that can be called multiple times (think recursion). The method is defined like this:
public void doCommit() { }
In order to tell it to fail I use this convention:
doThrow(new RuntimeException()).when(mMockedObject).doCommit();
This though, makes the method throw this exception EVERY time it is called. How can I make it so that it only, for example, throws it the first and third time it is called? Which means that, for example, the second and forth time it just returns without throwing an exception. Please note that I am not the author of doCommit(), nor do I have source code that I can change.

I figured it out (with some hints from Igor). This is how you stub consecutive void method calls:
doThrow(new RuntimeException()).doNothing().doThrow(...).doNothing().when(mMockedObject).doCommit();
thanks Igor!

Reading Stubbing Consecutive Calls doco, something like this might do it:
when(mMockedObject.doCommit())
.thenThrow(new RuntimeException())
.thenCallRealMethod()
.thenThrow(new RuntimeException())
.thenCallRealMethod();
If you don't want to actually call the underlying method then you should use thenAnswer instead of thenCallRealMethod method and provide an empty stub imlementation.

Related

How to avoid Collection.sort() method from executing in a test case?

Hi I am writing unit test cases for a program. In that program I am testing a certain method in which their is this method : Collections.sort(Arraylist object).
Its something like this.
public void abc(){
try{
Arraylist<object_class> object=some_method.get(list);
Collections.sort(object);
System.out.print("The whole functon is executing successfully") }
catch(exception e) {
system.out.print("error message") } }
the some_method.get(list)` method I am calling is sending a empty list so when Collections.sort() is called it goes to catch block and the rest code is not executed.
What can i do so that this Collections.sort() method is not called while test case is running.
PS- above code is only to explain the question and I cant make changes to main class
In test class I tried to use this method
Mockito.doNothing().when(collections.sort(Mockito.anyList()));//this is not working
So I tried this Mockito.doNothing().when(collections.class).sort(Mockito.anyList()); //this is also not working
I can return a mock list to object but I want to know if I can prevent Collections.sort() from executing.
Please help
It' simple if you want to execute Collections.sort() without any exception follow below steps:
1) Create a list object with dummy values in your test class and send it to main class
list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
2) Second step is make sure that your
some_method.get(list);
returns a ArrayList of objects,
3) As Collections class has static method sort, JVM will execute rest of the code as usual. And will not through any exception.
PS- If Mockito.doNothing() is not working try with PowerMockito.doNothing(), it might work and make sure that you have
1) #PrepareforTest(Collections.class)
2) PowerMockito.mockStatic(Collections.class);
3) PowerMockito.doNothing().when(Collections.sort(obj));
Hope it's useful.
This is wrong on many levels:
You are using a raw type there (by using ArrayList without a generic type parameter) - never do that!
If your test setup makes that other method return null, then don't manipulate "later" code - instead either avoid that null or make sure your production code can deal with it!
In other words: there are only two reasonable choices:
A) if in reality, that method never returns null. Then you should make sure, that this is also true for your test setup.
B) if in reality, that method returns null, too ... then your production code needs to deal with that, for example by doing a null check before calling sort!
Finally: especially for lists, that problem really doesn't exist at all: the answer is - you never ever return null! Instead, you return an empty list if there is no data. Empty lists can be sorted without a problem! Long story short: avoid returning null in the first place.
I can return a mock list to object but I want to know if I can prevent Collections.sort() from executing.
I don't see a way to prevent that
Collections.sort() is executed as long as the method that calls it is executed and as long as you can't change the tested code. The reason is that sort is a static method. You can pass or inject an instance that is a mock to achieve your goal.
Returning a mock list that can be sorted is the way to go in my opinion. If you manipulate the behavior of the tested class during test you will have a higher risk not to find errors. Additionally, you have a tight coupling between test and tested class in this case.

How to assert that mocked method calls happen in-order with testify?

The documentation for AssertExpectations says "AssertExpectations asserts that everything specified with On and Return was in fact called as expected. Calls may have occurred in any order." What if I want to assert that some calls happen in-order?
gomock has *Call.After(*Call) for this, but I can't see anything similar in testify. Is there a way, or should I just use gomock?
Testify does not support this feature currently, but it's tracked in this issue https://github.com/stretchr/testify/issues/741.
Testify's mock.Mock object provides access to the ordered calls through the Calls property (https://pkg.go.dev/github.com/stretchr/testify/mock#Mock), which you could use to build this functionality yourself.

Compile time check for consecutive functions call

Suppose we have a class like this:
class OProcess {
...
void Process1();
void Process2(); // call only if Process1 wasn't called
...
}
such that function Process2() can be called only when function Process1() has NOT been called already.
Is there a way to check that Process class is used correctly at compile-time? I.e. compiler must give an error if Process1() CAN BE called before Process2() for some instance of OProcess object.
P.S. I understand that there can be code like this:
if (variable == 1000)
Process1();
Process2();
and compiler can't be sure that Process1() will be called before Process2(). But here compiler can be sure that Process1() CAN be called before Process2() for some values of variable. And I need it to make an error or at least warning.
The short answer is Somewhat.
The long answer is: C++ does not implement Linear Typing, thus uniqueness checks cannot be done at compile-time (fully). Still, reading this description gives us a trick: to implement this in the compiler, language designer forbid aliasing and enforce consumption.
So, if you agree that some runtime checks are allowed, then this can be done by having processes consume the object:
class OProcess {
public:
};
std::unique_ptr<OProcessed1> process1(std::unique_ptr<OProcess> op);
std::unique_ptr<OProcess> process2(std::unique_ptr<OProcess> op);
Where OProcessed1 is a proxy over OProcess presenting a restricted interface that exposes only those operations allowed on OProcess after that Process1 was called.
The runtime part of the checks is that:
void func(std::unique_ptr<OProcess> op) {
process1(std::move(op));
process2(std::move(op));
}
will compile, even though it is undefined behavior to do anything other than destruction/assignment to op after moving from it.
The correct way to do it is either make init private and reduce the risk you mention,
or use dependency injection, as 'init' methods, or any logic at all inside the constructor, are bad practice in terms of clean code
Another trick is to have ProcessBase that defines init and calls it in it's constructor. ProcessBase's constructor is called before the derived constructor, thus making sure that init is called before any logic is made in the derived class.
Edit:
You may want to change the logic to have both methods private and have one method called process3() that will call the other methods in the correct order.
Another option is to use the decorator design pattern and wrap one method in a class and have your decorator call them by order.

EasyMock expected void

Just trying the EasyMock for the first time.
I seem to get it going but I am immediately halted with the fact that the mocked class runs a method "returning" void (EntityManager.remove(abc)).
I am able to mock the EntityManger partly to begin testing, i.e.
EasyMock.expect(this.mockManager.find(Some.class, id)).andReturn(mock);
, but how do I do the same for the 'remove' case?
I can't do (for example):
EasyMock.expect(this.mockManager.remove(rek)).andReturn(Boolean(true));
And if I do nothing, I get:
java.lang.AssertionError:
Unexpected method call EntityManager.remove(EasyMock for class my.package.Some)...
I need to test the logic before getting to remove part, but I don't care if it actually succeeds (would be a different thing).
You don't need to call EasyMock.expect(). Just use
this.mockManager.remove(rek);
while in the recording phase (before calling replay()).
If you want the mocked method, for example, to throw an exception or to be called twice, use expectLastCal():
this.mockManager.remove(rek);
expectLastCall().andThrow(new RuntimeException());
//or expectLastCall().times(2);

gmock setting default actions / ON_CALL vs. EXPECT_CALL

I don't understand the difference between ON_CALL and EXPECT_CALL when using it to
specify the default action.
So far I noticed/learned there are two ways to adjust the default action of a mock:
ON_CALL(mock, methodX(_)).WillByDefault(Return(0x01));
or
EXPECT_CALL(mock, methodX(_)).WillRepeatedly(Return(0x01));
Could someone explain to me:
The difference between the two methods
The ups and downs of each one
When is it appropriate to use them (what kind of setup ...)
There are subtle but significant differences between the two statements. EXPECT_CALL sets expectation on a mock calls. Writing
EXPECT_CALL(mock, methodX(_)).WillRepeatedly(do_action);
tells gMock that methodX may be called on mock any number of times with any arguments, and when it is, mock will perform do_action. On the other hand,
ON_CALL(mock, methodX(_)).WillByDefault(do_action);
tells gMock that whenever methodX is invoked on mock, it should perform do_action. That feature is helpful in a scenario where you have to write many expectations on your mock, and most/all of them have to specify the same action -- especially if it's complex. You can specify that action in ON_CALL, and then write EXPECT_CALLs without specifying the action explicitly. E.g.,
ON_CALL(mock, Sign(Eq(0), _))
.WillByDefault(DoAll(SetArgPointee<1>("argument is zero"), Return(0)));
ON_CALL(mock, Sign(Gt(0), _))
.WillByDefault(DoAll(SetArgPointee<1>("argument is positive"), Return(1)));
ON_CALL(mock, Sign(Lt(0), _))
.WillByDefault(DoAll(SetArgPointee<1>("argument is negative"), Return(-1)));
Now, if you have to write a lot of EXPECT_CALLs, you don't have to mock's specify the behavior every time:
EXPECT_CALL(mock, Sign(-4, _));
EXPECT_CALL(mock, Sign(0, _));
EXPECT_CALL(mock, Sign(1, _)).Times(2);
EXPECT_CALL(mock, Sign(2, _));
EXPECT_CALL(mock, Sign(3, _));
EXPECT_CALL(mock, Sign(5, _));
In another example, assuming Sign returns int, if you write
ON_CALL(mock, Sign(Gt(0), _)).WillByDefault(Return(1));
EXPECT_CALL(mock, Sign(10, _));
the call mock.Sign(10) will return 1 as ON_CALL provides default behavior for a call specified by EXPECT_CALL. But if you write
EXPECT_CALL(mock, Sign(Gt(0), _).WillRepeatedly(Return(1));
EXPECT_CALL(mock, Sign(10, _));
the invocation of mock.Sign(10, p) will return 0. It will be matched against the second expectation. That expectation specifies no explicit action and gMock will generate a default action for it. That default action is to return a default value of the return type, which is 0 for int. The first expectation will be totally ignored in this case.
ON_CALL(mock, methodX(_)).WillByDefault(Return(0x01));
EXPECT_CALL(mock, methodX(_)).WillRepeatedly(Return(0x01));
As you said, these two lines are doing exactly the same thing, therefore there are no differences at all. Use either way to set a default action as you please.
However, there is a logical difference :
ON_CALL(mock, methodX(_)).WillByDefault(Return(0x01)); means that the method might be called, and if that happens, every call will return 0x01
EXPECT_CALL(mock, methodX(_)).WillRepeatedly(Return(0x01)); means that it is expected that the method will be called, and every call will return 0x01
By the way, there is a Setting default actions in their cheat sheet, which says :
To customize the default action for a particular method, use ON_CALL():
ON_CALL(mock_object, method(matchers))
.With(multi_argument_matcher) ?
.WillByDefault(action);
Here is the "official" explanation about the most important differences between ON_CALL and EXPECT_CALL as explained in the gMock Cookbook.
There are basically two constructs for defining the behavior of a mock object: ON_CALL and EXPECT_CALL.
The difference?
ON_CALL defines what happens when a mock method is called, but doesn't imply any expectation on the method being called.
EXPECT_CALL not only defines the behavior, but also sets an expectation that the method must be called with the given arguments, for the given number of times (and in the given order when you specify the order too).
Since EXPECT_CALL does more, isn't it better than ON_CALL?
Not really. Every EXPECT_CALL adds a constraint on the behavior of the code under test. Having more constraints than necessary is bad - even worse than not having enough constraints.
This may be counter-intuitive. How could tests that verify more be worse than tests that verify less? Isn't verification the whole point of tests?
The answer, lies in what a test should verify. A good test verifies the contract of the code. If a test over-specifies, it doesn't leave enough freedom to the implementation. As a result, changing the implementation without breaking the contract (e.g. refactoring and optimization), which should be perfectly fine to do, can break such tests. Then you have to spend time fixing them, only to see them broken again the next time the implementation is changed.
Keep in mind that one doesn't have to verify more than one property in one test. In fact, it's a good style to verify only one thing in one test. If you do that, a bug will likely break only one or two tests instead of dozens (which case would you rather debug?). If you are also in the habit of giving tests descriptive names that tell what they verify, you can often easily guess what's wrong just from the test log itself.
So use ON_CALL by default, and only use EXPECT_CALL when you actually intend to verify that the call is made.
See here https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#knowing-when-to-expect-useoncall
There are basically two constructs for defining the behavior of a mock object: ON_CALL and EXPECT_CALL. The difference? ON_CALL defines what happens when a mock method is called, but doesn't imply any expectation on the method being called. EXPECT_CALL not only defines the behavior, but also sets an expectation that the method will be called with the given arguments, for the given number of times (and in the given order when you specify the order too).
One difference is that the ON_CALL behavior (default behavior) and the EXPECT_CALL expectations are cleared differently.
https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md#verifying-and-resetting-a-mock
using ::testing::Mock;
...
// Verifies and removes the expectations on mock_obj;
// returns true if and only if successful.
Mock::VerifyAndClearExpectations(&mock_obj);
...
// Verifies and removes the expectations on mock_obj;
// also removes the default actions set by ON_CALL();
// returns true if and only if successful.
Mock::VerifyAndClear(&mock_obj);
This can be used to clear expectations at some point in your test, but still keep the default behavior of the mock object. Notice that this is not the case for StrictMock objects since they will not allow the test to pass without actual expectations, even with a defined default behavior setup with ON_CALL.