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.
Related
I have a few 3rd parties static util methods in my project, some of them just pass or throw an exception. There are a lot of examples out there on how to mock a static method using PowerMock but Junit5 doesn't support PowerMock, which has a return type other than void. But how can I mock a static method that returns void to just "doNothing()"?
Don't mock it. From your minimal description, you could replace the usage sites with Consumer and provide a mock of that. (You can even initialize the fields with method references to the static methods and just have setter methods to override them.)
However, the fact that the class under test calls these static methods is usually just an implementation detail that the test shouldn't care about, only checking observable behavior.
Build a wrapper class on it, call the wrapper method, and you can have a different wrapper in your test if you can do behavior differently or have the wrapper to call a class/method can be mocked ,
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();
Just wondering how do you mock a return object of type Class from a method using EasyMock or PowerMock?
I am trying to mock the return type of this interface method
Class<T> getRootBeanClass();
I tried the following but with no joy
EasyMock.expect(mockViolation.getRootBeanClass()).andReturn(EasyMock.anyObject());
I get this error message
java.lang.IllegalStateException: matcher calls were used outside expectations
Any help is greatly appreciated
anyObject() is an argument matcher.
You use it so that EasyMock can distinguish between different mocking specifications that you put up. Like:
EasyMock.expect(mock.foo(eq("A"), anyObject()).andReturn(resultForA);
versus
EasyMock.expect(mock.foo(eq("B"), anyObject()).andReturn(resultForB);
The object that you return must be a real existing object (which can be either mocked, or not mocked). But anyObject() does not create an object for you!
EasyMock can't generate generics for you - so would need a cast, something like:
Class<Foo> foo = (Class<Foo>) mock(Class.class)
But this doesn't work in this case - as java.lang.Class is final! And EasyMock can't mock final classes! You need PowerMock for that - or (my recommendation) the latest version of Mockito.
In case you go for Mockito: please read their documentation - as you have to enable mocking of final classes - it is an experimental feature as of now.
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
As i did some research i have found out that PowerMock is able to mock static java methods.
Can someone explain (technically) what is PowerMock doing different than JUnit and others which can not or do not? And also why static methods are(were) causing issues when they are tried to mock?
thanks
http://blog.jayway.com/2009/05/17/mocking-static-methods-in-java-system-classes/
In order to mock an instance method, you can simply override it in a subclass. You can't do that with static methods because there's no "static polymorphism".
Powermock can do it because it works with bytecode, while other popular frameworks rely on polymorphism and create subclasses with CGLIB.
From the link: "Basically all standard mock frameworks use CGLib to create a mock object which means that they're based on a hierarchical model (CGLib creates a sub class of the class to test at run-time which is the actual mock object) instead of a delegation model which PowerMock uses through it's byte-code manipulation by delegating to the MockGateway."