Using Gradle to run Automated Tests

Hello everyone,

For a plugin I am developing I have set up automated build scripts using gradle. This works well for the actual compilation and packaging of the plugin but fails to work for tests.

My unit tests currently look like this:

public class MethodChainIdentificationEngineTest extends LightIdeaTestCase {

    private MethodChainIdentificationEngine engine;

    public void setUp
() throws Exception {
= new MethodChainIdentificationEngine();

    public void testIdentifyMethodChainsReturnsIdentificationsInstance() throws Exception {
        PsiMethod method = getEmptyMethod();
IdentificationCollection<MethodChainIdentification> ids = engine.identifyMethodChains(method);

    public void testReturnsEmptyIdentificationsForEmptyMethod() throws Exception {
        assertEquals(0, engine.identifyMethodChains(getEmptyMethod()).size());

    public void testReturnsEmptyIdentificationsForOneCallMethod() throws Exception {
        String oneCallMethod = IOUtils.readFile("src/test/resources/psi/method/oneCallMethod.txt");
PsiMethod method = getJavaFacade().getElementFactory().createMethodFromText(oneCallMethod, null);
(0, engine.identifyMethodChains(method).size());

    public void testReturnsOneIdentificationForTwoCallMethod() throws Exception {
        String twoCallMethodClass = IOUtils.readFile("src/test/resources/psi/class/methodCallChainMethodClass.txt");
PsiClass clazz = getJavaFacade().getElementFactory().createClassFromText(twoCallMethodClass, null);
PsiMethod method = clazz.getMethods()[0];
(1, engine.identifyMethodChains(method).size());

    private PsiMethod getEmptyMethod() throws IOException {
        String emptyMethod = IOUtils.readFile("src/test/resources/psi/method/emptyMethod.txt");
        return getJavaFacade
().getElementFactory().createMethodFromText(emptyMethod, null);

Running these tests inside IntelliJ using its built-in test runner works flawlessly. However, using my build.gradle file it fails.

These are parts of my build.gradle (removed some irrelevant blocks):

buildscript {   repositories {     mavenCentral()     jcenter()     maven { url '' }   }   dependencies {     classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '3.0.1')     classpath 'net.saliman:gradle-properties-plugin:1.4.2'   } } repositories {     add buildscript.repositories.getByName("maven-main-cache") } apply plugin: 'com.jfrog.artifactory' apply plugin: 'maven-publish' apply plugin: 'java' apply plugin: '' apply plugin: 'checkstyle' apply plugin: 'jacoco' version = '0.0.1-SNAPSHOT' group = 'com.simonstuck' configurations {   antTask }
dependencies {   repositories {     mavenCentral()     jcenter()     flatDir dirs: 'lib'   }   dependencies {     checkstyle('')   }   compile 'org.jdom:jdom:1.1'   compile ''   compile fileTree(dir: "${idea_home}/lib", include: '*.jar')   antTask group: 'com.intellij', name: 'javac2', version: '12.1.0'   antTask group: 'com.intellij', name: 'forms_rt', version: '12.1.0'   antTask group: 'com.intellij', name: 'asm-all', version: '12.1.0-idea'   antTask group: 'org.jdom', name: 'jdom', version: '1.1'   testCompile "org.mockito:mockito-core:1.+" } task compileJava(overwrite: true, dependsOn: configurations.compile.getTaskDependencyFromProjectDependency(true, 'jar')) {   doLast {       project.sourceSets.main.output.classesDir.mkdirs()       ant.taskdef name: 'javac2', classname: 'com.intellij.ant.Javac2', classpath: configurations.antTask.asPath       ant.javac2 srcdir:':'),       classpath: project.sourceSets.main.compileClasspath.asPath,       destdir: project.sourceSets.main.output.classesDir,       source: sourceCompatibility,       target: targetCompatibility,       includeAntRuntime: false   } } jar {   metaInf {     from ("${rootDir}/META-INF") {       include 'plugin.xml'     }   } }

When I run ./gradlew build I get the following output:

21:01:46.469 [DEBUG] [TestEventLogger] com.simonstuck.vignelli.inspection.identification.MethodChainIdentificationEngineTest > testReturnsEmptyIdentificationsForEmptyMethod STANDARD_ERROR 21:01:46.471 [DEBUG] [TestEventLogger]     ERROR: com/sun/jdi/Value 21:01:46.475 [DEBUG] [TestEventLogger]     java.lang.NoClassDefFoundError: com/sun/jdi/Value 21:01:46.475 [DEBUG] [TestEventLogger]          at java.lang.Class.forName0(Native Method) 21:01:46.476 [DEBUG] [TestEventLogger]          at java.lang.Class.forName(

I presume this is either due to not compiling the test files themselves with the correct SDK or because I need to use a special test runner that I don't know about.
Do you happen to have any insights into why this does not work?



Comment actions Permalink

You need to add %JDK_HOME%/lib/tools.jar to the execution classpath for your tests.

Comment actions Permalink

Thank you – that was it!


Please sign in to leave a comment.