EasyMock has a function called createMockBuilder with which someone can specify a partially mocked class.
Is it possible to do the same with Mockito?
For example in EasyMock some can do the following :
classA mockedA = EasyMock.
createMockBuilder(A.class).
withConstructor(B.class,C.class).
withArgs(b,null).
addMockedMethod("print").
createMock();
Is it possible to do the same with Mockito?
Mockito does it a little differently from EasyMock. For instance, whereas in EasyMock, you decide which member functions you want mocked:
EasyMock.createMockBuilder(A.class).addMockedMethod("foo");
in Mockito, all member functions are mocked by default, and you can specify when you want to call an underlying function:
A a = Mockito.mock(A.class);
Mockito.when(a.foo()).thenCallRealMethod();
If you're wanting to mock only a few member functions with Mockito, I can think of two ways to proceed: The example above, and spying.
A a = Mockito.spy(A.class);
Mockito.when(a.foo()).thenReturn("ret");
a.bar(); // Calls the real A.bar() function.
Using a spy, member functions are not mocked by default, but can be mocked selectively. See more information here: http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html#spy
Related
I am using junit to run a few unit tests. One of these calls a method in an object that I mock using mockito like;
#Mock
private MyClass myClass;
I then set up mockito to do something like
Mockito.when(myClass.foo(Mockito.any()).thenReturn(bar);
Now myClass.foo actually takes another one of my classes (say class Person) as an argument and what I would like to do is something like this
Mockito.when(myClass.foo(Person parson)).thenDo(person.setName("Name")).thenReturn(bar);
That is of course pseudo code but I hope it illustrates what I am trying to do. Is this possible?
You need to use thenAnswer or its twin doAnswer method.
See Mockito : doAnswer Vs thenReturn
You should use thenReturn or doReturn when you know the return value at the time you mock a method call. This defined value is returned when you invoke the mocked method.
Answer is used when you need to do additional actions when a mocked method is invoked, e.g. when you need to compute the return value based on the parameters of this method call.
If your answers become too complicated, consider using a fake instead of a mock.
In this case if your goal is to set field of Person object you can do that before or after the line:
Mockito.when(myClass.foo(Mockito.any()).thenReturn(bar);
doAnswer() would help do operations based on input, but operations would be performed on copy of arguments not the original arguments.
I need to mock out a class due to one third party required dependency, so I can unit test it (I cannot remove this dependency).
So what I've done is mocked out the class and then utilized Mockito.doRealMethod() for all the function calls, but I feel this sort of abstracts what is being really done.
When in reality I want the real class and just want to mock out that one call.
Partial mocking of a class is supported via spy in Mockito. See the Mockito documentation on partial mocks for more information.
Another possibility may be to use org.mockito.Mockito.CALLS_REAL_METHODS, such as:
YourClass YOUR_MOCK = Mockito.mock( YourClass.class, CALLS_REAL_METHODS );
You can override the real method on a spy like
Mockito.doReturn("foobar").when(mySpy).myMethod();
My class under test is ClassA which in one of its private method uses a static factory (lets say CarFactory) method getCar(XXX xxx) which returns a Car.
Part of the CarFactory logic is to validate that the given xxx parameters meets some criteria.
I tried using Mockito like the following:
#Mock private Car mockForCar;
#Mock private XXX xxxMock;
...
when(CarFactory.getCar(xxxMock)).thenReturn(mockForCar);
But I get an exception regarding that xxxMock isn't valid by the CarFactory.
Why is the real getCar(xxx) method gets called and not the stubbed one?
Is there a better way doing this?
From the Mockito FAQ:
Can I mock static methods?
No. Mockito prefers object orientation and dependency injection over
static, procedural code that is hard to understand & change. If you
deal with scary legacy code you can use JMockit or Powermock to mock
static methods.
If you want to stub this using Mockito, you should make it non-static and inject the factory. And that's better OO design anyway.
I want to mock a class from other library using Mockito. I read that Mockito relies on specific (CGLIB provided I think) implementation of equals method. Unfortunately this outer class has equals() denoted with final modifier, and there is throwing exception in its body.
When I try to mock this class I always get exception from this method. CGLIB apparently doesn't get by with final, and real method is called.
Any ideas? What can I do, to mock this class using Mockito? Maybe other library will handle it?
[EDIT] quick explanation: I don't want to mock equals(), I check other methods. Problem is that mockito internally uses equals(), I don't know what for. As equals() is final, real method is called with exception throwing. I had hope that there is some setting in mockito "don't use equals()" :-)
Thanks for answers, I will read them closely tomorrow.
This matrix shows features supported by different frameworks:
External link to the matrix here.
According to this, only PowerMock and JMockit can mock final methods.
Mockito cannot mock final methods. Apparently PowerMock can though.
A hacky workaround could be to create a non-final method that delegates to the final equals method and mock that.
I believe that the steps to mock a final method with PowerMock and Mockito API would be: run your tests with the #RunWith(PowerMockRunner.class) then prepare the class you want to mock #PrepareForTest(ClassToBeMocked.class). After that, mock your object and use the when method to mock the equals method.
I think that it won't work if you do not use the PrepareForTest annotation in your test class.
I need to mock a public, non-static method of an object.
The problem is that I ca'nt create a mock object, because this object is created directly in the code.
I have tried spying the class using PowerMockito.spy() and PowerMockito.when(...) but it didn't work (maybe its because PowerMockito.when only works for static and private methods)
For example, suppose I need to test this:
...
myClass anObject = new myClass();
anObject.aMethod();
...
How could I mock the call anObject.aMethod() ??
I guess I need to spy myClass, but it didn't work..
Use dependency injection.
In simplest case, just pass the factory object to the method that creates your object, and then spy that factory.
Other way of doing this is to pass the factory to the constructor of your object.
It's likely best to refactor your code to use dependency injection.
But a quick google suggests that you can actually stub the constructor in PowerMockito. See javadoc for whenNew.
I can't vouch for it as I haven't used PowerMockito, but this looks like it should allow you to make your constructor call return a mock object.