Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test context reading 'wrong' application properties file #534

Closed
pablo-parra opened this issue Dec 19, 2018 · 16 comments
Closed

Test context reading 'wrong' application properties file #534

pablo-parra opened this issue Dec 19, 2018 · 16 comments
Labels
Milestone

Comments

@pablo-parra
Copy link

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

@rutcreate
Copy link

rutcreate commented Dec 21, 2018

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.

@jdneo
Copy link
Member

jdneo commented Jan 2, 2019

@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

@pablo-parra
Copy link
Author

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!! :)

@yaohaizh
Copy link
Contributor

yaohaizh commented Jan 4, 2019

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

@pablo-parra
Copy link
Author

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

@jdneo
Copy link
Member

jdneo commented Jan 6, 2019

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

@jdneo
Copy link
Member

jdneo commented Feb 25, 2019

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.

@jdneo
Copy link
Member

jdneo commented Feb 26, 2019

Fixed in #629 and will be released within 0.15.0

@jdneo jdneo closed this as completed Feb 26, 2019
@pmverma
Copy link

pmverma commented Mar 2, 2019

Having same problem, waiting for next release!

@jdneo jdneo added the bug label Mar 6, 2019
@tnatanael
Copy link

Same problem here!

@EdgarArguelles
Copy link

issue is still happening in version 0.30.0

@jdneo
Copy link
Member

jdneo commented Jun 7, 2021

@EdgarArguelles Could you share a sample project?

@fvclaus
Copy link

fvclaus commented Apr 1, 2022

@jdneo This bug is reproducible with Gradle, but not with Maven. I tried a small Spring project with both Gradle and Maven, but was unable to reproduce it with Maven. The project can be found here: https://github.com/fvclaus/vscode-java-test-application-properties.

I have two application.properties with the same property properties-location which is either main or test.
Using Gradle on the command line, the test succeeds:

dev@ZBM-WN-61626:~/workspace/java-test-application-properties-gradle$ gradle test

BUILD SUCCESSFUL in 2s
4 actionable tasks: 4 executed

If I execute it in vscode, the test fails:

image

I believe it is because the order of the main and test classpath is flipped:

-cp /home/dev/workspace/java-test-application-properties-gradle/bin/main:/home/dev/workspace/java-test-application-properties-gradle/bin/test...

As a workaround, I currently use:

		"java.test.config": [
			{
				"name": "local",
				"workingDirectory": "${workspaceFolder}",
				"env": { 
					"spring.config.location": "${workspaceFolder}/src/test/resources/",
				},
			},
		]

@jdneo
Copy link
Member

jdneo commented Apr 2, 2022

@fvclaus, it's a known issue that the classpath resolved by Gradle(Buildship) is wrong. In the future we will consider to add a feature that delegate the test execution to Gradle (that will make sure the result is the same as running from command line)

See: #1045 (comment)

@Ortega-Dan
Copy link

Confirming the issue is still present. Looks like it will be fixed with #1045 but the workaround shared here by @fvclaus works very well. Thanks.

omarkohl added a commit to omarkohl/wagetracker that referenced this issue Mar 13, 2024
The wrong application.properties file is used so the tests try to
connect to a PostgreSQL DB. When executing the tests on the command line
with gradle there is no issue.

Bug and workaround:
microsoft/vscode-java-test#534 (comment)
@jdneo
Copy link
Member

jdneo commented Aug 5, 2024

The Gradle Test Delegation (both run and debug) has supported now.

To use this feature, you need to install the latest Test Runner for Java and Gradle for Java extension.

To delegate the tests to Gradle, you can set the default testing profile in Testing explorer:
image
image

If you do not want to change the default testing profile, you can trigger an one-time execution via:
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants