I've seen a few questions out there regarding this but I can't seem to make sense of any of the answers for my particular problem.
I have a mock object, lets call "object1", which I send to some method for testing, lets call testMethod(). So I end up calling
testMethod(object1);
for testing. Now somewhere in this testMethod, there will be a part where it calls a method
object1.toggleDisplay();
which is a void method. If the method were like
object1.getDisplay()
where it actually returns something, I usually do
EasyMock.expect(object1.getDisplay()).andReturn(whatever);
However, this is a void method, and I would like to just test that this has been indeed been called for a certain amount of times. What is the easiest way to do this?
Thanks
If things haven't changed in the last few years, you use expectLastCall when setting up your expectations.
object1.toggleDisplay();
object.expectLastCall();
object1.toggleDisplay();
EasyMock.expectLastCall().times(5);
or if you import statically the EasyMock methods:
import static org.easymock.EasyMock.*;
[...]
object1.toggleDisplay();
expectLastCall().times(5);
Related
So I'm trying to test a method methodUnderTest which is something like this:
methodUnderTest{
//Other stuff that works nice
this.setterMethod(OtherClass.methodThatErrors(arg1));
}
So I'm trying to avoid that static method call methodThatErrors.
What I'm doing so far:
ClassUnderTest spy = Mockito.spy(objectOfClassUnderTest);
Mockito.doNothing().when(spy).setterMethod(Mockito.any(OtherClass.class));
However this does not work, the method is still being called. I would've thought that it shouldn't call the argument method at all because I've written any for the argument of setterMethod. So how can I prevent this call using Mockito, not PowerMockito.
Edit:
So I managed to mock it by moving the static method call like so:
methodUnderTest{
this.extraSetterMethod(arg1);
}
extraSetterMethod(arg1){
this.setterMethod(OtherClass.methodThatErrors(arg1));
}
I don't really like this solution because it's adding several extra lines of code (even more with javadoc) which just makes the class messier.
The problem here is that the value is coming directly from the static method, and not the setter itself. Whatever value it's getting is coming from the real invocation of the method itself. Mocking out the call to the setter has no effect; it's not the result of the setter that's in error.
This is a scenario in which you have to make a hard decision:
Introduce a parameter to the method so that the call can be easier to mock, or
Use PowerMockito to mock out the call.
I strongly encourage the latter, especially if you're dealing with a legacy code base which is not very forgiving to a signature change.
This may be a simple question, but I could not find an answer with reasonable search.
I am trying to make a static method return a value more than once in multiple tests. I fail to achieve this with the mocked static method with PowerMock. To put this simply I have a JUnit test #BeforeClass like this
#RunWith(PowerMockRunner.class)
#PrepareForTest(StaticStuff.class)
public class MyTest extends TestCase {
#BeforeClass
public static void init() {
// Mock some stuff
PowerMockito.mockStatic(StaticStuff.class);
Mockito.when(StaticStuff.get()).thenReturn("something");
}
}
Now, this works for the first test accessing the static method, but the rest will receive 'null' (update: or any other value that the "real" method would return). I can fix the problem simply changing the #BeforeClass to #Before, thus making the static mock be recreated for every test. But afaik, this is not the correct way to do this. For this particular case this will do, but I believe there should be a way to provide information that the method may be called "any times".
Actually I understood from the documentation that the same value should be returned "infinite times by default" (but it doesn't. From Mockito documentation: 'Once stubbed, mocked method will always return stubbed value regardless of how many times it is called.'). Also I would expect that stating the amount of calls should be something this simple (but it isn't):
Mockito.when(StaticStuff.get()).thenReturn("something").times(10);
Maybe I am just missing something?
The tests should be independent from each other. If you use JUnit, the order of the tests is not determined at all (see https://github.com/junit-team/junit/wiki/Test-execution-order). I think creating mocks in #BeforeClass is actually a bad practice, since it can cause that the tests are depending on each other. So I would simply recommend to use #Before method to initialize all mocks, and use #BeforeClass only, if you prepare something really common for the tests (like a connection pool or something like that).
I'm trying to wrap my head around mockito and was wondering how I would test if a method calls a method!
So here is class with its method inside it:
public class RegisterController {
public void regHandle(UserDataObject user1){
ValidateRegisterInputController validate = new ValidateRegisterInputController();
validate.validateInputHandle(user1); }
How would I test that regHandle(UserDataObject) calls validate.validateInputHandle(user1); ?
I'm sure this is a super simple test, but I really can't figure out how to test this.
There are various ways of writing a test for a method which instantiates some other class. I wrote about two of them in my article on the Mockito wiki, at http://code.google.com/p/mockito/wiki/MockingObjectCreation
Both the techniques that I describe involve refactoring your code to make it more testable.
You would create a mock of ValidateRegisterInputController and then pass it on construction, then you would do:
Mockito.verify(mock).validateInputHandle(user1).
I strongly suggest you do not do this type of testing though. Instead of that, ask yourself how can you write an unit test that checks that what you wanted to validate was valid.
for example, check that after calling regHandle user1.isValid() is equals to true.
So I am using easymock to write unit test fpr a method inside my class. I have mocked an object inside this method and it works fine. The problem is inside this test method I have several calls to other methods inside the same class.
For example,
public class testClass {
public void testMethod() {
mockedObject.method(); --> I define a return type for this and it works fine.
this.otherMethod() {
--> Inside this method I have a call to the mocked object with different methods..
}
}
So my question is that, when the method calls another method by using this.otherMethod() should I define all the return values for the mockedObject calls inside otherMethod? Isn't there a way to specify a return value for otherMethod so that it won't even go inside the other method? Plz note that here this is not the mockedObject and that is why I cannot use eaymock return value for that. I was thinking maybe there might be a way around this.
Thanks.
Update: I found this for partial mocking of methods: http://www.easymock.org/EasyMock2_2_2_ClassExtension_Documentation.html
Which helps to only mock specific methods. But when I try to use it, it crosses a line on createMock method.. Sth like wrong syntax!
Thanks everyone,
I found this link: http://fczaja.blogspot.com/2011/11/easymock-create-partial-mocks.html
The answer is to partially mock those methods called within this method to isolate those method calls.
Thanks again.
I was thinking, is it possible to mock whole object behavior with EasyMock, but in a way that once declared mock with all expected values and results is used several times without caring about the order of the requests ?
The purpose for this is to create an instance of mock for example in JUnit test #BeforeClass and use it in several #Test methods.
Thank you in advance for any input,
Regards,
P.
If you are not interested in verifying calls to the mock, and your only aim is to ensure that whenever a specific method on the mock is called, it will always return the same desired result, you can configure it using andStubReturn(), e.g.
expect(mock.getMeaningOfLifeUniverseAndEverything()).andStubReturn(42);
I think you're really wanting two things:
The ability to use mocked methods out of order, which is the default easymock (non-strict mode),
The ability to use mocked methods any number of times.
You do the latter like this:
expect(someMock.someMethod()).anyTimes().andReturn(someValue);
If your method will get different arguments each time it is called, you can use the anyObject() method to ignore the provided argument.