4

A test that works in a .java file throws an exception when put into a .groovy file. Why is that?

  • Groovy: 2.3.9
  • JMockit: 1.20
  • Java: 1.8.0_60

The code:

@Test
public void testSystemCurrentTimeMillis(@Mocked final System unused) {
    new NonStrictExpectations() {{
        System.currentTimeMillis(); result = 1438357206679L;
    }};
    long currentTime = System.currentTimeMillis();
    Assert.assertEquals(1438357206679L, currentTime);
}

The exception:

java.lang.IllegalArgumentException: No class with name "java_lang_System$currentTimeMillis" found

    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at x.GroovyTest$1.<init>(GroovyTest.groovy:12)
    at x.GroovyTest.testSystemCurrentTimeMillis(GroovyTest.groovy:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
  • 1
    My initial thought is that the groovy parser gets confused with the `{{ ... }}` block in regards to a closure or block. – tylerwal Dec 15 '15 at 03:07
  • @tylerwal (I'm a Groovy noob:) Is there anything I can do to confirm / prevent that? – Evgeniy Berezovsky Dec 15 '15 at 07:09
  • You can force the treatment of a block by a label: `MyLabel:{ ... }`; try enclosing the innermost block with a label. – tylerwal Dec 15 '15 at 12:40
  • @tylerwal I tried enclosing `System.currentTimeMillis(); result = 1438357206679L;`, and then `long currentTime = System.currentTimeMillis(); Assert...` inside label blocks, to no avail. – Evgeniy Berezovsky Dec 15 '15 at 23:27
  • 1
    I wasn't familiar with this syntax (`{{ ... }}`), but I found a good link explaining it: [DoubleBraceInitialization](http://www.c2.com/cgi/wiki?DoubleBraceInitialization) - I'm assuming there is a groovy way that does the same thing. – tylerwal Dec 16 '15 at 00:32

0 Answers0