Vscode-java-test: Test context reading 'wrong' application properties file

Created on 19 Dec 2018  路  10Comments  路  Source: microsoft/vscode-java-test

In a Spring Boot sample project using a MySQL db as main data source and H2 as test data source, when launching the test using Java Test Runner extension the application properties file read by the app is the one located in the "main" package instead the one located in the "test" package. Due to that all the test fail as are not finding the expected infrastructure.

The test of the project are working fine in Eclipse.

application_properties_issue

bug

Most helpful comment

Hi @jdneo, I have followed the steps you mention and it's working now 馃憤.
I guess it is something that could be done automatically by the extension or at least should be mentioned in the "Usage" section of the extension landing page.

Thanks for your help!! :)

All 10 comments

Same problem here. If there is any workaround please share.

For my case, I use DBRider to test database. It will delete tables in database before or after start test case. From this problem cause dev's database tables will be deleted instead of test's database.

For my workaround is run command line mvn test instead of click Run Test on VSCode.

@pablo-parra @rutcreate

It might have a better solution to this issue. For now, you can try the following workaround:

  1. Check if the application.properties is in /target/test-classes/
  2. If yes, then open your launch.test.json file, in env field, add the following setting: "spring.config.location": "classpath:/target/test-classes/"
  3. Do not forget to set the default field as well, otherwise, the extension will not use the configuration as the default.

Here is an example:

{
    "run": {
        "default": "todo-app-java-on-azure",
        "items": [
            {
                "name": "todo-app-java-on-azure",
                "projectName": "todo-app-java-on-azure",
                "workingDirectory": "c:\\Users\\sheche\\Desktop\\todo-app-java-on-azure-master",
                "args": [
                ],
                "vmargs": [],
                "env": {
                    "spring.config.location": "classpath:/target/test-classes/"
                },
                "preLaunchTask": ""
            }
        ]
    },
    // ...
}

Please let me know if this could help with your problem.

Thanks

Hi @jdneo, I have followed the steps you mention and it's working now 馃憤.
I guess it is something that could be done automatically by the extension or at least should be mentioned in the "Usage" section of the extension landing page.

Thanks for your help!! :)

@jdneo, please document this either on readme or wiki page.

I'm doing now further testing and seems that the workaround is fixing the issue only partially. With the referred configuration "spring.config.location": "classpath:/target/test-classes/" in the _env_ property we avoid reading the \main\resources\application.properties and the default configurations (H2 in my case) are applied. The problem is that now the context is not reading apparently any customized application.properties file so extra properties are ignored. I have checked it creating a new test profile (_application-test.properties_ file) and trying to activate the profile with the spring.profiles.active=test property in \test\resources\application.properties. When launching the test during the application startup the logs confirm that the profile (and the related application properties file) is ignored: No active profile set, falling back to default profiles.

Following the Spring Boot 2.1.0-RELEASE documentation (that is my project framework version) I have tried adding additional properties to _env_ specification such as "spring.config.additional-location": "classpath:/target/test-classes/" and "spring.config.name": "application.properties" but with no success

@pablo-parra Thanks, I'll look into it.

Sorry for the late reply.

After some investigation, I found out that it is because the classpath generated from the Java Language Server will put the target/classes higher than target/test-class, thus failed to load the correct appllication.properties for testing.

I'll figure out if there is any solution to correct the order.

Fixed in #629 and will be released within 0.15.0

Having same problem, waiting for next release!

Same problem here!

Was this page helpful?
0 / 5 - 0 ratings