vscode-java-test icon indicating copy to clipboard operation
vscode-java-test copied to clipboard

Run test fails with gradle when tasks that process resources are involved.

Open hrsto opened this issue 5 years ago • 9 comments

Inside src/test/resources there is an arquillian.xml file that has to be expanded by gradle at processTestResources task. When a test is run from within vs code, this doesn't happen and the resource file is used as is. This triggers an error from Junit that basically says that some property is invalid (because it's expanded, i.e. junit see it as ${somePropToExpand}).

Same thing occurs when arquillian.xml is auto generated by a plugin - a gradle plugin can auto create it and put it directly in the buildDir/resources/test dir. When a test is run from within vs code, basically the file is invisible to vs code.

Both of these problems can be corrected by having a:

sourceSets {
    test {
        resources {
            srcDirs += "$buildDir/resources/test"
        }
    }
}

But that by itself creates a new set of problems for the build.

hrsto avatar Aug 12 '20 14:08 hrsto

The project classpath is resolved by buildship (if it's a Gradle project). To help us better understand the problem, would you mind to share a sample project for this issue to us?

jdneo avatar Aug 13 '20 08:08 jdneo

Attaching the most minimal i could produce.

There's only one test class - ArquillianInitTest.

First do a gradle clean test. It should complete successfully.

This initial normal build is set to auto generate arquillian.xml at built time, thus: /tmp/sample/src/test/resources/__arquillian.xml is prefixed with __ to ignore it. Such a build doesn't work when executed from vscode, because vscode code thinks this file doesn't exist. But it can be found in $buildDir/resources/test.

Now remove the __ prefix from /tmp/sample/src/test/resources/__arquillian.xml to force gradle to explicitly use it. Make sure you manually set the wlpHome (in my case, project ran from /tmp). The following is observed:

  1. when you do gradle clean test it should complete successfully.
  2. when run from vscode, it will error out because of the properties the need to be expanded. For ex., this is the error i get: java.lang.NumberFormatException: For input string: "${SERVER_PORT}"
  3. if you run gradle processTestResources, you can confirm the property expansion works - the files in $buildDir/resources/test have the proper values.
  4. src/test/resources/arquillian.xml and src/test/resources/persistence.xml have commented out lines - toggle those so that you directly set the values. When run from vscode it will complete successfully.

sample.tar.gz

hrsto avatar Aug 13 '20 20:08 hrsto

Here are some findings:

I can repro this issue and I also tried the project in Eclipse and IntelliJ.

In Eclipse, same error when running the test, but it can successfully run if I use the Delegate to Gradle feature.

In IntelliJ, since it will delegate to Gradle by default, so the test runs successfully.

Seems that we need to support Delegate to Gradle at VS Code side.

jdneo avatar Aug 20 '20 03:08 jdneo

I have the same problem. A "Delegate to Gradle" Feature would be great!

egvimo avatar Feb 22 '21 07:02 egvimo

Any update on this?

ytreister avatar Aug 15 '22 13:08 ytreister

I have the same problem. A "Delegate to Gradle" Feature would be great!

prbpedro avatar Sep 06 '22 12:09 prbpedro

I tried to call "gardle test" with vscode-gradle but could only get as far as the call.

import * as util from "util";
import * as vscode from "vscode";
import { ExtensionApi as GradleApi, RunTaskOpts, Output } from "vscode-gradle";

function runGradleTest(context: IRunTestContext) {
    const extension = vscode.extensions.getExtension("vscjava.vscode-gradle");
    if (extension == null) {
        return null;
    }

    const gradleApi = extension!.exports as GradleApi;
    const testMethods: TestItem[] = [];
    const visitTestItem = (testItem: TestItem) => {
        if(testItem.id.indexOf('#') > 0 ){
            testMethods.push(testItem);
        } else {
            testItem.children.forEach(visitTestItem)
        }
    }
    context.testItems.forEach(visitTestItem);

    return new Promise(async (resolve, reject) => {
        while(testMethods.length) {
            const testMethod = testMethods.shift();
            if(testMethod==null){
                continue;
            }

            const taskId = testMethod.id;
            const runTaskOpts: RunTaskOpts = {
                projectFolder: context.workspaceFolder.uri.fsPath,
                taskName: "test",
                args: [
                    "--tasks",
                    taskId.replace(/.+@/, '').replace('#', '.'),
                ],
                showOutputColors: false,
                onOutput: (output: Output): void => {
                    const message = new util.TextDecoder("utf-8").decode(
                        output.getOutputBytes_asU8()
                    );
                    console.log(output.getOutputType(), message);
                },
            };
            await gradleApi.runTask(runTaskOpts);
        }
        resolve(null);
    });
}

uk-taniyama avatar Nov 23 '22 12:11 uk-taniyama