I have some code that has a static method inside a final class. I was trying to mock that method. I have tried doing a few things..
public final Class Class1{
public static void doSomething(){
}
}
How can I mock doSomething()? I have tried..
Class1 c1=PowerMockito.mock(Class1.class)
PowerMockito.mockSatic(Class1.class);
Mockito.doNothing().when(c1).doSomething();
This gives me an error:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.cerner.devcenter.wag.textHandler.AssessmentFactory_Test.testGetGradeReport_HTMLAssessment(AssessmentFactory_Test.java:63)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
Most used testing framework is JUnit 4. So if you are using it you need to annotate test class with:
#RunWith( PowerMockRunner.class )
#PrepareForTest( Class1.class )
Than
PowerMockito.mockSatic(Class1.class);
Mockito.doNothing().when(c1).doSomething();
Mockito.when(Class1.doSomething()).thenReturn(fakedValue);
// call of static method is required to mock it
PowerMockito.doNothing().when(Class1.class);
Class1.doSomething();
I use PowerMock. It let's you do things Mockito can't do.
https://github.com/powermock/powermock/wiki
#RunWith(PowerMockRunner.class)
#PrepareForTest(StaticClass.class)
public class StaticTest {
#Before
public void setUp() {
PowerMockito.mockStatic(Bukkit.class);
//When a static method with no arguments is called.
when(StaticClass.callMethod1()).thenReturn(value);
//When a static method with an argument is called.
when(StaticClass.callMethod2(argument)).thenReturn(value2);
//Use when you don't care what the argument is..
//use Mockito.anyInt(), Mockito.anyDouble(), etc.
when(StaticClass.callMethod3(Mockito.anyString())).thenReturn(value3);
}
#Test
public void VerifyStaticMethodsWork() {
assertEquals(value, StaticClass.callMethod1());
assertEquals(value2, StaticClass.callMethod2(argument));
assertEquals(value3, StaticClass.callMethod3("Hello"));
}
}
Related
I need to mock the behavior of a static method inside a non-static factory class. The implementation of the class is:
ABCFactory.java
public class ABCFactory extends BaseUserFactory
{
private static final ABCFactory factory = new ABCFactory();
public static final ABCFactory getFactory()
{
return factory;
}
public Context getContext(String authority)
{
return (Context)createInstance(authority);
}
private ABCFactory()
{
}
protected Class getInterface()
{
return ABCFactory.class;
}
}
Now, this class is used in my code to get the profile something like:
Document.java:
Profile profile = ABCFactory.getFactory().getContext(authority).currentProfile();
I need to mock the ABCFactory class so that I can send my own context/profile object as a return type while testing. I've tried a bunch of methods but nothing seems to work here. Here's what I tried in my junit test class.
Try 1:
DocumentTest.java
ABCFactory mockABCFactory = Mockito.mock(ABCFactory.class);
ServiceProviderRegistrar.getRegistrar().bind(ABCFactory.class).toMockInstance(mockABCFactory);
Mockito.when(mockABCFactory .getFactory()).thenReturn(null);
Mockito.when(mockABCFactory .getContext(domain)).thenReturn(null);
Error:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods cannot be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
Try 2: (Using PowerMock to avoid the new call.
DocumentTest.java
ABCFactory mockABCFactory = Mockito.mock(ABCFactory.class);
ServiceProviderRegistrar.getRegistrar().bind(ABCFactory.class).toMockInstance(mockABCFactory);
try
{
PowerMockito.whenNew(ABCFactory.class).withNoArguments().thenReturn(mockABCFactory);
PowerMockito.when(ABCFactory.getFactory()).thenReturn(mockABCFactory);
}
catch (Exception e)
{
e.printStackTrace();
}
Mockito.when(mockABCFactory.getContext(domain)).thenReturn(null);
Error:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods cannot be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:490)
Try 3: (Used PowerMock.mockStatic)
DocumentTest.java
ABCFactory mockABCFactory= Mockito.mock(ABCFactory.class);
ServiceProviderRegistrar.getRegistrar().bind(ABCFactory.class).toMockInstance(mockABCFactory);
try
{
PowerMockito.whenNew(ABCFactory.class).withNoArguments().thenReturn(mockABCFactory);
PowerMockito.mockStatic(ABCFactory.class);
PowerMockito.when(ABCFactory.getFactory()).thenReturn(mockABCFactory);
}
catch (Exception e)
{
e.printStackTrace();
}
Mockito.when(mockABCFactory.getContext(domain)).thenReturn(null);
Error:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods cannot be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:490)
What am I missing here. I have tried several other ways but ABCFactory.getFactory() always returns a new object but not my mocked object. How do I mock the behavior of ABCFactory class without changing its implementation?! Please help.
Did you use following annotations.
#RunWith(PowerMockRunner.class)
#PrepareForTest( ABCFactory.class )
I tried and following code works.
DocumentTest.class
#RunWith(PowerMockRunner.class)
#PrepareForTest( ABCFactory.class )
public class DocumentTest
{
/** Unit under test. */
private Document user;
#Before public void setUp() {
user = new Document();
ABCFactory abc = ABCFactory.getFactory();
PowerMockito.mockStatic(ABCFactory.class);
PowerMockito.when(ABCFactory.getFactory()).thenReturn(abc);
}
#Test public void testABC() {
assertEquals("", user.useFactory() );
}
}
Document class
public class Document
{
public String useFactory(){
String profile = ABCFactory.getFactory().getContext("");
return profile;
}
}
I am using PowerMockito and jUnit to write unit test cases.
public class Foo {
private String resolveApplicationId() {
return "testApplication";
}
}
Here is my test case
#RunWith(PowerMockRunner.class)
#PrepareForTest(Foo.class)
public class test{
#Before
public void prepareTest() {
foo = PowerMockito.spy(new Foo());
}
#Test
public void checkApplicationIdIsResolved() throws Exception {
PowerMockito.doNothing().when(foo, "myPrivateMethod");
PowerMockito.verifyPrivate(foo).invoke("myPrivateMethod");
//Assert Here the returned value
}
}
Please tell me
1. how can I assert the value returned by the method when it is called
2. how can I call the private method
3. if not then what actually I verify when I write test case for private methods.
Thanks.
Testing private method does not differ from testing public method. If there is no external dependencies you even don't need to create and use any mocks. The only problem is with invocation of the private method from test. This is described here or you may use spring utils.
So you don't need to mock the method you are testing. You only require to mock other objects which are not tested in this particular test. So you test would look like
#Test
public void checkApplicationIdIsResolved() throws Exception {
// makeResolveIdAccessible();
// if needed setup mocks for objects used in resolveApplicationId
assertEquals(expectedApplicationId, foo.resolveApplicationId())
}
I am using PowerMock to mock static methods in junit tests, typically done as follows:
#RunWith(PowerMockRunner.class)
#PrepareForTest({Foo.class,Bar.class})
public class SomeUnitTest {
#Before
public void setUpTest() {
setUpFoo();
setUpBar();
}
private void setUpFoo() {
mockStatic(Foo.class);
when(Foo.someStaticMethod()).thenReturn(1);
}
private void setUpBar() {
mockStatic(Bar.class);
when(Bar.someStaticMethod()).thenReturn(2);
}
#Test
public void someTestCase() {
...
}
}
This works fine, but I'm finding that specifying the #PrepareForTest annotation is preventing me from making my testing API flexible.
What I'd like to do is something like the following:
public class MockLibraryOne {
public static void setUpLibraryOne() {
setUpFoo();
setUpBar();
}
private static void setUpFoo() {
mockStatic(Foo.class);
when(Foo.someStaticMethod()).thenReturn(1);
}
private static void setUpBar() {
mockStatic(Bar.class);
when(Bar.someStaticMethod()).thenReturn(2);
}
}
#RunWith(PowerMockRunner.class)
public class SomeUnitTest {
#Before
public void setUpTest() {
MockLibraryOne.setUpLibraryOne();
}
#Test
public void someTestCase() {
...
}
}
Here my unit test has a dependency on LibraryOne, but it does not know which classes LibraryOne depends on, so it does not know which classes to add to the #PrepareForTest annotation.
I could make SomeUnitTest extend MockLibraryOne and add the #PrepareForTest annotation to the MockLibraryOne class, but I will have dependencies on more than just MockLibraryOne in other unit tests, so inheritance is not a general solution.
Is there some way of programmatically preparing a class for testing under PowerMock, instead of using the #PrepareForTest annotation? For example, something like the following:
public class MockLibraryOne {
public static void setUpLibraryOne() {
setUpFoo();
setUpBar();
}
private static void setUpFoo() {
prepareForTest(Foo.class);
mockStatic(Foo.class);
when(Foo.someStaticMethod()).thenReturn(1);
}
private static void setUpBar() {
prepareForTest(Bar.class);
mockStatic(Bar.class);
when(Bar.someStaticMethod()).thenReturn(2);
}
}
I guess it would be nice if PowerMockRunner processed the #PrepareForTest annotation a little differently: for each specified class, it should not only add that class (and its hierarchy) to the list of classes to prepare for mocking, but then examine that class to see if it has any #PrepareForTest annotations as well:
#RunWith(PowerMockRunner.class)
#PrepareForTest({MockLibraryOne.class})
public class SomeUnitTest {
...
}
#PrepareForTest({Foo.class,Bar.class})
public class MockLibraryOne {
...
}
}
So in this the #PrepareForTest annotation on SomeUnitTest would find MockLibraryOne, and the #PrepareForTest annotation there would drag in Foo.class and Bar.class as well.
So perhaps writing my own test runner to replace PowerMockRunner may be a solution.
Or perhaps there's a simpler solution, using PowerMockAgent class, for example?
edit: Mock Policies may be one solution: https://code.google.com/p/powermock/wiki/MockPolicies
edit: Mock Policies works with PowerMockRunner but not (it seems) with PowerMockRule (which I sometimes require due to class loader issues).
What you try to achieve will not work.
The problem is that powermock must rewrite the client class's code to intercept the static invocation and it can't do this after the class is loaded. Thus it can only prepare a class for test before it is loaded.
Let's assume you want to mock the System.currentTimeMillis invocation in the following simple class.
class SystemClock {
public long getTime() {
return System.currentTimeMillis();
}
}
Powermock will not change the code of java.lang.System.currentTimeMillis, because it can't. Instead it changes the SystemClock's byte code so that it does not invoke System.currentTimeMillis anymore. Instead it invokes some other object that belong to powermock.
This is how powermock get's full control over the return value and allows you to write a test like this:
#RunWith(PowerMockRunner.class)
#PrepareForTest({ SystemClock.class })
public class PowerMockitoTest {
#Test
public void systemTimeMillis() {
SystemClock systemClock = new SystemClock();
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.currentTimeMillis()).thenReturn(12345L);
long time = systemClock.getTime();
assertEquals(12345L, time);
}
}
You can see that powermock has rewritten the client class in the stacktrace of your debugger. Set a breakpoint at SystemClock.getTime and step into the invoked method.
As you can see SystemClock invokes a MockGateway.
If you take a look at the variables on the stack of the MockGateway invocation, you can see how the original System.currentTimeMillis method is handled.
Perhaps you're looking for a mock policy?
Could you help this (taken from documentation)?
You can also prepare whole packages for test by using wildcards:
#PrepareForTest(fullyQualifiedNames="com.mypackage.*")
So you can add the whole library to your prepare...
Why do you even want to mock static methods? Why not wrap those static methods in a class that you can mock with mockito?
class FooWraper {
void someMethod() {
Foo.someStaticMethod()
}
}
and then you can create a mock of your FooWraper. No need to use Powermock at all...
I want to mock a static method and also non-static methods of a class.
My source looks like:
public class XDSUtilityManager
{
private static XDSUtilityManager xdsUtilMgr = new XDSUtilityManager();
private XDSUtilityManager()
{
xdsUtilMgrImpl = new XDSUtilityManagerImpl();
}
public static XDSUtilityManager getInstance()
{
return (xdsUtilMgr == null ) ? new XDSUtilityManager() : xdsUtilMgr;
}
public XMLDocument getXMLDocument(final String absoluteKey, final XDSClient xdsClient)
{
return getXMLDocument(absoluteKey, xdsClient, false);
}
}
I want to mock static method getInstance(). I want getInstance() to return mock object of XDSUtilityManager class. Also I want to mock getXMLDocument() which is not static.
And in my testCase I tried following:
XMLDocument xmlDocument = PowerMock.createMock(XMLDocument.class);
XDSUtilityManager xdsUtilityManager = PowerMock.createPartialMock(XDSUtilityManager.class,"getXMLDocument");
PowerMock.mockStaticPartial(XDSUtilityManager.class, "getInstance");
expect(XDSUtilityManager.getInstance()).andReturn(xdsUtilityManager).anyTimes();
expect(xdsUtilityManager.getXMLDocument((String)anyObject(), anyObject(XDSClient.class))).andReturn(xmlDocument).anyTimes();
PowerMock.replay(xdsUtilityManager);
PowerMock.replay(xmlDocument);
But things are not working as expected. Please help
The easiest way I have found to do this is by using PowerMockito. PowerMockito is a combination of Mockito and PowerMock, which allows the mocking of static objects.
The pattern I have used is to use the mock your static getInstance() to return a copy of your non static mock, which you then extend as normal. An example using PowerMockito would be:
#RunWith(PowerMockRunner.class)
#PrepareForTest({SingletonObject.class})
public class SingletonTester {
#Mock
private SingletonObject singleMock;
#Before
public void setup(){
// initialize all the #Mock objects
MockitoAnnotations.initMocks(this);
// mock all the statics
PowerMockito.mockStatic(SingletonObject.class);
}
#Test
public void mockTester(){
// Mock the static getInstance call to return the non-Static Mock
Mockito.when(SingletonObject.getInstance()).thenReturn(singleMock);
// Mock the non static version as normal
PowerMockito.when(singleMock.nonStaticMethodCall()).thenReturn("Whatever you need.");
//..........
}
}
Your static call to getInstance() to get the singleton object returns the instantiated mock object you ALSO define. Once you tell the static what to return, you can continue to mock the non-static calls as normal.
Its not required to call getInstance() method to create the instance of XDSUtilityManager.
Using PowerMockito, as your constructor is private you need to suppress the constructor before you call create mock as below..
PowerMockito.suppress(PowerMockito.constructor(XDSUtilityManager.class));
// enable static mocking for all the methods in the class
PowerMockito.mockStatic(XDSUtilityManager.class);
XDSUtilityManager xDSUtilityManager = PowerMockito.mock(XDSUtilityManager.class);
XMLDocument xmlDocument = PowerMock.createMock(XMLDocument.class);
PowerMockito.when(xDSUtilityManager.getXMLDocument(
Mockito.anyString(),Mockito.anyObject())).thenReturn(xmlDocument);
I am using Mockito 1.9 with Grails 1.3.7 and I have a strange bug.
The following test case in java works:
import static org.mockito.Mockito.*;
public class MockitoTests extends TestCase {
#Test
public void testSomeVoidMethod(){
TestClass spy = spy(new TestClass());
doNothing().when(spy).someVoidMethod();
}
public static class TestClass {
public void someVoidMethod(){
}
}
}
This test in groovy does not work:
import static org.mockito.Mockito.*
public class MockitoTests extends TestCase {
public void testSomeVoidMethod() {
def testClassMock = spy(new TestClass())
doNothing().when(testClassMock).someVoidMethod()
}
}
public class TestClass{
public void someVoidMethod(){
}
}
This is the error message:
only void methods can doNothing()!
Example of correct use of doNothing():
doNothing().
doThrow(new RuntimeException())
.when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called
org.mockito.exceptions.base.MockitoException:
Only void methods can doNothing()!
Example of correct use of doNothing():
doNothing().
doThrow(new RuntimeException())
.when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called
at org.codehaus.groovy.runtime.callsite.CallSiteArray.createPogoSite(CallSiteArray.java:129)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:146)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
Does anymone observed the same error?
The problem is Groovy is intercepting your method call before it reaches someVoidMethod. The method actually being called is getMetaClass which is not a void method.
You can verify this is happening by replacing:
doNothing().when(testClassMock).someVoidMethod()
with:
doReturn(testClassMock.getMetaClass()).when(testClassMock).someVoidMethod()
I'm not sure you will be able to get around this issue using stock Mockito and Groovy.