Gradle and JMockit


(vrablik) #1

Hi,

I have created test class with JMockit. The test fails during executing in gradle, but works in eclipse and in Maven.

I am getting this exception: java.lang.IllegalStateException: Invalid context for the recording of expectations

at mockit.internal.expectations.RecordAndReplayExecution.validateRecordingContext(RecordAndReplayExecution.java:60)

at mockit.internal.expectations.RecordAndReplayExecution.(RecordAndReplayExecution.java:91)

at mockit.Expectations.(Expectations.java:138)

at org.apache.catalina.session.ispn.InfinispanSessionManagerTest$1.(InfinispanSessionManagerTest.java:40)

at org.apache.catalina.session.ispn.InfinispanSessionManagerTest.stripJvmRoute1(InfinispanSessionManagerTest.java:40)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)

at org.testng.internal.Invoker.invokeMethod(Invoker.java:691)

at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883)

at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1208)

at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)

at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)

at org.testng.TestRunner.privateRun(TestRunner.java:753)

at org.testng.TestRunner.run(TestRunner.java:613)

at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)

at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)

at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)

at org.testng.SuiteRunner.run(SuiteRunner.java:241)

at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)

at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)

at org.testng.TestNG.runSuitesSequentially(TestNG.java:1169)

at org.testng.TestNG.runSuitesLocally(TestNG.java:1094)

at org.testng.TestNG.run(TestNG.java:1006)

at org.gradle.api.internal.tasks.testing.testng.TestNGTestClassProcessor.stop(TestNGTestClassProcessor.java:95)

at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:58)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)

at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)

at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:75)

at $Proxy7.stop(Unknown Source)

at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:91)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)

at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)

at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:75)

at $Proxy7.stop(Unknown Source)

at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:91)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)

at org.gradle.messaging.remote.internal.TypeCastDispatch.dispatch(TypeCastDispatch.java:30)

at org.gradle.messaging.remote.internal.WorkerProtocol.handleIncoming(WorkerProtocol.java:53)

at org.gradle.messaging.remote.internal.WorkerProtocol.handleIncoming(WorkerProtocol.java:31)

at org.gradle.messaging.remote.internal.ProtocolStack$ProtocolStage.handleIncoming(ProtocolStack.java:167)

at org.gradle.messaging.remote.internal.ProtocolStack$BottomStage.handleIncoming(ProtocolStack.java:277)

at org.gradle.messaging.remote.internal.ProtocolStack$BottomConnection$1.run(ProtocolStack.java:299)

at org.gradle.messaging.remote.internal.ProtocolStack$ExecuteRunnable.dispatch(ProtocolStack.java:120)

at org.gradle.messaging.remote.internal.ProtocolStack$ExecuteRunnable.dispatch(ProtocolStack.java:116)

at org.gradle.messaging.dispatch.AsyncDispatch.dispatchMessages(AsyncDispatch.java:132)

at org.gradle.messaging.dispatch.AsyncDispatch.access$000(AsyncDispatch.java:33)

at org.gradle.messaging.dispatch.AsyncDispatch$1.run(AsyncDispatch.java:72)

at org.gradle.messaging.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

at java.lang.Thread.run(Thread.java:662)

My env: linux, java6 update 26, gradle milestone 6 (tested with m3 too), tested with testNG (6.2.1 and 6.3) and tested with junit 4.10 JMockit 0.999.11

My unit tests works fine in Eclipse 3.6 and Maven 3 than I think it is gradle problem.

Here is my build.gradle file content:

group = 'gradle'
version = 'prototype'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
  configurations {
}
  repositories {
//
 flatDir(dirs: "$projectDir/lib")
   mavenRepo(urls: "https://repository.jboss.org/nexus/content/groups/public/")
   mavenCentral()
}
  dependencies {
    //tomcat dependencies
    compile 'org.apache.tomcat:catalina:6.0.29'
    compile 'org.apache.tomcat:coyote:6.0.29'
    compile 'org.apache.tomcat:juli:6.0.29'
      //infinispan dependencies
    infinispanVersion = '5.0.1.FINAL'
    compile([group: 'org.infinispan', name: 'infinispan-core', version: infinispanVersion]) {
      artifact {
         name = "infinispan-core"
         type = 'jar'
      }
    }
      testCompile 'com.googlecode.jmockit:jmockit:0.999.11'
    testCompile 'org.testng:testng:6.2.1'
}
  jar.enabled = true
  test {
    useTestNG()
    jvmArgs
= ['-javaagent:/home/zvrablik/devel/jmockit/jmockit/jmockit.jar']
}

Do I have invaild gradle configuration or it is a gradle bug?

Thanks.

Regards, Zdenek


(Doug Lethin) #2

Starting with testng 6.2, you only need to have the testng.jar on the classpath. You don’t need to specify a javaagent on the command line OR register a testng listener. So, your test configuration should just look like:

test {

useTestNG() }

What’s curious in your case is that you are passing -javaagent jvm property referencing some jmockit.jar file not managed by gradle. So its possible that that version of jmockit you have deep in your user directory is not compatible with version 0.999.11 you are pulling down with gradle.

I also use gradle (1.0-milestone-6), testng (6.1.1) and jmockit (0.999.11) . There is an incompatibility with gradle and testng 6.2.x as I outline in this bug report (http://issues.gradle.org/browse/GRADLE-1822) which forces me to use an earlier version o testng. Because I don’t use the latest version of testng, its not enough to just have testng on the classpath to run the tests. I also need to register a testng listener as outlined in the jmockit documentation (http://jmockit.googlecode.com/svn/trunk/www/tutorial/RunningTests.html).

This is the configuration that is working for me:

dependencies {
  // there is a bug I reported to gradle (http://issues.gradle.org/browse/GRADLE-1822)
  // where when using testng 6.2.x, any exceptions thrown in methods annotated with
  // @Before[Test,Method,Class] will NOT cause the test task to fail.
  // For now, just using an earlier version, as we really don't need the latest
  //testCompile "org.testng:testng:6.2.1"
  testCompile "org.testng:testng:6.1.1"
  testCompile "com.googlecode.jmockit:jmockit:0.999.11"
  testCompile "junit:junit:4.9"}
}
  test {
   useTestNG()
     options {
    // until we get on to testng 6.2 (which we can't because of GRADLE-1822), we have to
    // register this testn listener to ensure jmockit is initialized correctly.
    listeners << 'mockit.integration.testng.Initializer'
  }
}

Hope this helps.


(vrablik) #3

Build including JMockit tests works fine after downgrade to testng 6.1.1

Thank you.

Regards, Zdenek