microsoft / vscode-java-test

Run and debug Java test cases in Visual Studio Code.
https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-test
Other
297 stars 132 forks source link

Testing with coverage breaks with method references #1668

Closed heruan closed 9 months ago

heruan commented 9 months ago

I'm using the Java Extensions Pack (pre-release) with Code Insiders and I'm experiencing a weird behavior with the new "Testing with coverage".

Suddenly I started getting this error when running Test with coverage:

Failed to load test coverage: Error: Illegal argument: line must be non-negative

So I inspected my latest changes and started commenting every line out until I've found two lines that once commented, get rid of the error.

    static class RadioButtonLayout extends VerticalLayout {

        private final RadioButtonGroup<RadioButtonOption> radioButtonGroup = new RadioButtonGroup<>();

        public RadioButtonLayout(String title, String description) {
            radioButtonGroup.setItemLabelGenerator(RadioButtonOption::getLabel); // <== THIS LINE ==|
        }
    }

and

        var map = PropertyMapper.get().alwaysApplyingWhenNonNull();
        map.from(properties::getClientId).to(builder::clientId);
        map.from(properties::getClientSecret).to(builder::clientSecret);
        map.from(properties::getClientAuthenticationMethod)
                .as(ClientAuthenticationMethod::new)
                .to(builder::clientAuthenticationMethod);
        map.from(properties::getAuthorizationGrantType)
                .as(AuthorizationGrantType::new)
                .to(builder::authorizationGrantType);
        map.from(properties::getRedirectUri).to(builder::redirectUri);
        map.from(properties::getScope)
                .as(StringUtils::toStringArray) // <-- THIS LINE ==|
                .to(builder::scope);
        map.from(properties::getClientName).to(builder::clientName);
        return builder.build();

Commenting out just the two <-- THIS LINE ==| lines makes the "Testing with coverage" to run fine.

Notice that the two lines are on completely unrelated source files and the test run fails even if the test class does not involve any of those files. Both lines contains method references: there are other lines with method references that do not affect the test run, but these two lines have specific Type::method reference, while others are Type::ctor or instance::method.

Let me know if I can provide any other information. Here's my environment:

Environment

Test Runner for Java

Version: v0.40.2024011806 (pre-release)

Visual Studio Code

Version: 1.87.0-insider (Universal) Commit: c11a2dd4d52e38cb92b8c464f47a7b02bb7c8762 Date: 2024-02-24T00:18:27.042Z Electron: 27.3.2 ElectronBuildId: 26836302 Chromium: 118.0.5993.159 Node.js: 18.17.1 V8: 11.8.172.18-electron.0 OS: Darwin arm64 23.2.0

heruan commented 9 months ago

Surprisingly enough, if I change this line

radioButtonGroup.setItemLabelGenerator(RadioButtonOption::getLabel);

to

radioButtonGroup.setItemLabelGenerator(option -> option.getLabel());

then it works, but if I change the other line from

.as(StringUtils::toStringArray)

to

.as(scope -> StringUtils.toStringArray(scope))

it still fails. Looks like it has something to do with type references in lambdas 🤔

jdneo commented 9 months ago

Hi @heruan, thank you for reporting the issue.

I tried simply add some lambda code like

List<String> l = new ArrayList<>();
l.add("a");
l.stream().forEach(System.out::println);

but it still works.

Is it possible to share a sample project for this issue? It will help me a lot to fix the bug :)

heruan commented 9 months ago

Thanks for the feedback! Sure, here's the minimum code that I've been able to strip down to reproduce the issue: https://gist.github.com/heruan/817705978ad33926147ad8780785af4b

I've noticed that both line 11 and 16 have to be there to trigger the error, commenting either of those the test run fine.

jdneo commented 9 months ago

Thank you @heruan!

The snippets are very helpful! The issue is fixed with https://github.com/microsoft/vscode-java-test/pull/1639/commits/4b6ccb67a45fea0fdb3a93be5e1fd2c84f550f83.

I'll schedule a new pre-release recently for this bug.

heruan commented 9 months ago

Thanks @jdneo! I'll test the pre-release once out. Out of curiosity, what's the cause of this?

jdneo commented 9 months ago

Thank you, I'll let you know once the pre-release is out.

Some methods(lambda) miss debug information in the compiled class files. In such case, Jacoco returns -1 for the line of the methods and causes the issue.

jdneo commented 9 months ago

Hi @heruan,

Would you mind trying this bit to see if the issue is fixed? vscode-java-test-0.40.2024030408.vsix.zip

heruan commented 9 months ago

I confirm this fixes the issue on my project, thanks!

jdneo commented 8 months ago

Great! I'll schedule a pre-release then.