Mocking System.nanoTime() cause StackOverflowError #317

Closed
honza889 opened this Issue Aug 6, 2016 · 0 comments

Projects

None yet

2 participants

@honza889
honza889 commented Aug 6, 2016 edited

Affected JMockit version: 1.26 (example works correctly with 1.22 and 1.24)

Mocking of System.nanoTime() fails because using this method in URLClassLoader - mocking of time cause StackOverflowError during classloading. (Mocking of other System methods works ok.)

The same problem occure in version 1.24 if I omit @RunWith(JMockit.class) annotation.

@RunWith(JMockit.class)
public class MyTest {
    @Test
    public void test() {
        System.out.println(System.nanoTime());
        new MockUp<System>() {
            @Mock
            public long nanoTime(){
                return 1234;
            }
        };
        System.out.println(System.nanoTime());
    }
}
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844

java.lang.StackOverflowError
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at mockit.internal.mockups.MockMethodBridge.invoke(MockMethodBridge.java:32)
 -> at java.lang.System.nanoTime(System.java)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:444)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at mockit.internal.mockups.MockMethodBridge.invoke(MockMethodBridge.java:32)
 -> at java.lang.System.nanoTime(System.java)
    ...
@rliesenfeld rliesenfeld added the bug label Aug 6, 2016
@rliesenfeld rliesenfeld self-assigned this Aug 6, 2016
@rliesenfeld rliesenfeld added a commit that closed this issue Aug 6, 2016
@rliesenfeld rliesenfeld Re-added the pre-loading of certain classes, which otherwise would ca…
…use a StackOverflowError when called from a mocked JRE class; closes #317.
4a61552
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment