PowerMock with Gradle

Hello again,

This week I am trying to migrate a legacy system to gradle and having a lot of problems.

Now, I am trying to run unit test that use powermock calling a ANT script from gradle:

    configurations {
        junitAnt
    }
 
   dependencies {
         compile(group: 'mockito-all', name: 'mockito-all', version: '1.9.5')
         compile(group: 'org.powermock', name: 'powermock-core', version: '1.6.1')
         compile(group: 'org.powermock', name: 'powermock-mockito', version: '1.6.1', classifier: 'full')
         compile(group: 'objenesis', name: 'objenesis', version: '2.1')
         compile(group: 'org.javassist', name: 'javassist', version: '3.18.0-GA')
         compile(group: 'cglib-nodep', name: 'cglib-nodep', version: '2.2.2')
         compile(group: 'br.com.six2six', name: 'fixture-factory', version: '2.1.0')
       
         junitAnt(group: 'junit', name:'junit', version:'4.12')
    
         junitAnt(group:'org.apache.ant', name:'ant-junit', version:'1.9.5'){
                  transitive = false    
         }
    
         ant.taskdef(name: 'junit', classname: 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTask', classpath: configurations.junitAnt.asPath)

       
       junitAnt(group:'org.apache.ant', name:'ant-junit4', version:'1.9.5'){
              transitive = false
       }
}

And then import the ANT script and call an ANT task:

ant.importBuild ‘build.xml’

When I run the build.xml stand-alone the tests run perfectly. But when a call this same scritp from the Gradle script, and the unit test use powermock, like this example:

@RunWith(PowerMockRunner.class)
@PrepareForTest({AbstractProcessador.class, ProcessadorCadastro.class, GenericDAO.class})
public class ProcessadorComissaoTest

The folow error appears:

Testcase: initializationError took 0,002 sec
    Caused an ERROR
Failed to transform class with name br.ufrn.arq.dao.Database. Reason: [source error] no such constructor: java.sql.SQLException
java.lang.IllegalStateException: Failed to transform class with name br.ufrn.arq.dao.Database. Reason: [source error] no such constructor: java.sql.SQLException
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:266)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:180)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
...
Caused by: javassist.CannotCompileException: [source error] no such constructor: java.sql.SQLException
    at javassist.expr.NewExpr.replace(NewExpr.java:213)
    at org.powermock.core.transformers.impl.MainMockTransformer$PowerMockExpressionEditor.edit(MainMockTransformer.java:418)
    at javassist.expr.ExprEditor.loopBody(ExprEditor.java:212)
    at javassist.expr.ExprEditor.doit(ExprEditor.java:91)
    at javassist.CtClassType.instrument(CtClassType.java:1398)
    at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:74)
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:251)
Caused by: compile error: no such constructor: java.sql.SQLException
    at javassist.compiler.MemberCodeGen.atMethodCallCore2(MemberCodeGen.java:593)
    at javassist.compiler.MemberCodeGen.atMethodCallCore(MemberCodeGen.java:575)
    at javassist.expr.NewExpr$ProceedForNew.doit(NewExpr.java:236)
    at javassist.compiler.JvstCodeGen.atCallExpr(JvstCodeGen.java:235)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
    at javassist.compiler.CodeGen.atAssignCore(CodeGen.java:860)

When I call unit tests without powermock the test run perfectly.

I know that powermock run with other classload and make a lot of byte code manipulation, and probably this error is related to classload or byte code manupulation.

I have tried many changes, but no solution until now. I have changed javassist version to 3.18.0-GA, 3.19.0-GA and
3.20.0-GA without success.

If someone have alredy run powermock tests in this situation and can help me, I will appreciate.

Tkanks.

PS.: I am calling an ANT script from Grande to compile and run the tests because it was the solution that I found for another problem discribe in this topic:

I am having a similar problem. I am trying to enable PowerMock functionality in my tests and as a first step just added @RunWith(PowerMockRunner.class) and the tests still run in the local workspace, but when trying to execute it on build server it fails:

Successfully started process 'Gradle Worker 2’
Gradle Worker 2 executing tests.
Before running test info: test initializationError(com.experiments.FooTest)
Gradle Worker 2 finished executing tests.

com.experiments.FooTest > initializationError FAILED
java.lang.IllegalStateException: Failed to transform class with name com.experiments.FooTest. Reason: Invalid index

    Caused by:
    sun.misc.InvalidJarIndexException: Invalid index

Any ideas what might be wrong here?