_From [email protected] on February 19, 2015 16:22:29_
Please see the unit test below, the last assertion should not fail.
These versions are defined in Gradle:
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.powermock:powermock-module-junit4:1.6.1'
testCompile 'org.powermock:powermock-api-mockito:1.6.1'
import android.content.Context;
import android.text.format.DateFormat;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* Created by Arnold Pistorius on 19-2-2015.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Calendar.class, DateFormat.class})
public class StaticTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
mockStatic(DateFormat.class);
mockStatic(Calendar.class);
when(DateFormat.getTimeFormat(any(Context.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return new SimpleDateFormat("hh:mm");
}
});
when(Calendar.getInstance()).thenCallRealMethod();
}
@Test
public void testTest() {
// Check if DateFormat.getTimeFormat(...) returns a SimpleDateFormat
assertTrue(DateFormat.getTimeFormat(mock(Context.class)) instanceof SimpleDateFormat);
assertTrue(DateFormat.getTimeFormat(mock(Context.class)) instanceof SimpleDateFormat);
// Check if Calendar.getInstance() returns a Calendar instance
assertTrue(Calendar.getInstance() instanceof Calendar);
// Check again if DateFormat.getTimeFormat(...) returns a SimpleDateFormat
assertTrue(DateFormat.getTimeFormat(mock(Context.class)) instanceof SimpleDateFormat);
}
}
_Original issue: http://code.google.com/p/powermock/issues/detail?id=542_
_From [email protected] on February 20, 2015 00:47:02_
It seems for all assertions shouldMockThisCall in MockGateway.java:174 returns true, except for the last one (after thenCallRealMethod() is called).
The putAdditionalState("DontMockNextCall", true) in MockRepository.java:238 is called when callRealMethod is called on a non final system class (MockitoMethodInvocationControl.java:239).
The Javadoc says this is needed to prevent infinite recursion. But maybe this isn't the case for static methods???
@johanhaleby any update on this issue, I'm in the same situation, thanks!
Any update we are running into this issue as well. Or is there a possibility of a work around.
I am facing this same issue, seems not being fixed
I am in pwermock-api-mockito2: 1.7.3
Most helpful comment
_From [email protected] on February 20, 2015 00:47:02_
It seems for all assertions shouldMockThisCall in MockGateway.java:174 returns true, except for the last one (after thenCallRealMethod() is called).
The putAdditionalState("DontMockNextCall", true) in MockRepository.java:238 is called when callRealMethod is called on a non final system class (MockitoMethodInvocationControl.java:239).
The Javadoc says this is needed to prevent infinite recursion. But maybe this isn't the case for static methods???