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
293 stars 126 forks source link

annotation processor does not run correctly before running the test. #1508

Closed uk-taniyama closed 1 year ago

uk-taniyama commented 1 year ago

NoSuchBeanDefinitionException is raised and the test fails to run.

To work around this, the results of the gradle run are added to classPaths. Is there a better way to specify classPaths, as the control of classPaths becomes more complicated when we have subprojects.

settings.json:

  "java.test.defaultConfig": "custom",
  "java.test.config": [
    {
      "preLaunchTask": "compileJava",
      "name": "custom",
      "classPaths": [
        "${workspaceFolder}/build/classes"
      ]
    }
  ]

tasks.json:

{
  "tasks": [
    {
      "label": "compileJava",
      "type": "shell",
      "command": "gradlew compileJava",
      "options": {
        "cwd": "${workspaceFolder}"
      }
    }
  ]
}
uk-taniyama commented 1 year ago

I tried copy to bin by gradle. It is working in the environment at hand. I am forcing a copy to the bin directory, but does that seem to be a problem?

settings.json:

  "java.test.defaultConfig": "custom",
  "java.test.config": [
    {
      "preLaunchTask": "compileJava",
      "name": "custom",
  //    "classPaths": [
  //     "${workspaceFolder}/build/classes"
  //    ]
    }
  ]

tasks.json:

{
  "tasks": [
    {
      "label": "compileJava",
      "type": "shell",
      "command": "gradlew copyToBin",
      "options": {
        "cwd": "${workspaceFolder}"
      }
    }
  ]
}

build.gradle:

task copyToBin(type: Copy) {
  dependsOn compileJava
  from 'build/classes/java/main'
  into 'bin/main'
}
jdneo commented 1 year ago

The problem sounds like that the compiled output from language server and gradle command line are different.

Is it possible to share a sample project?

uk-taniyama commented 1 year ago

I am trying it out on this project.

https://github.com/uk-taniyama/spring-doma

Turning "preCheckTask" on and off can be controlled in the Makefile.

image

jdneo commented 1 year ago

We are actually working on the annotation processor support for the Gradle project: https://github.com/eclipse/eclipse.jdt.ls/pull/2319

And I can see the generated sources: image

But after I imported the project, I saw some other errors: image

Anything I missed?

uk-taniyama commented 1 year ago

We are actually working on the annotation processor support for the Gradle project: https://github.com/eclipse/eclipse.jdt.ls/pull/2319

Can i use this one?

Anything I missed?

".sql" files are stored under resources. In gradle, i control the ability to reference under resources.

ext.domaResourcesDir = "${buildDir}/tmp/doma-resources"

task extractDomaResources(type: Copy, dependsOn: processResources)  {
  from processResources.destinationDir
  include 'doma.compile.config'
  include 'META-INF/**/*.sql'
  include 'META-INF/**/*.script'
  into domaResourcesDir
}

task copyDomaResources(type: Copy, dependsOn: extractDomaResources)  {
  from domaResourcesDir
  into compileJava.destinationDir
}

It would simply solve the problem if the "gradle build" was executed.

jdneo commented 1 year ago

Can I use this one?

It will be available in the pre-release version once it's merged. I directly launched with that PR to test the sample project.

I manually copied the META-INF folder into bin/default and triggers a full build, then I could see the code generation works well: image

There are a few test cases fail, but I guess that are related with some environment issues? image

Anyway, I'll let you know once the upstream change is merged.

uk-taniyama commented 1 year ago

It will be available in the pre-release version once it's merged. I directly launched with that PR to test the sample project. I manually copied the META-INF folder into bin/default and triggers a full build, then I could see the code generation works well:

Is there any good way to make this place automatic?

Would copying it with preLaunchTask solve the problem? It would be faster than the current "gradle build" call.

There are a few test cases fail, but I guess that are related with some environment issues?

sorry. this is my problem.

DaoTestBase.java jdbc url is "mem", but not setup db in test.

    // url = "jdbc:h2:mem:db;MODE=PostgreSQL;DB_CLOSE_DELAY=-1";
    url = "jdbc:h2:file:./work/db/db;MODE=MySQL";
jdneo commented 1 year ago

Is there any good way to make this place automatic?

It should be only needed for once I think, because the incremental build should not remove them. But you may need to do it again if the content is changed.

As you may have noticed that the extension compiles the class file into bin folder instead of the build folder (which is the target folder when running gradle build from command line). Many problems are caused by this, and I think the META-INF folder in your project is another example. We will consider fixing this in the future.

For the annotation processor, looks like the PR mentioned above is very promising.

uk-taniyama commented 1 year ago

Is there any good way to make this place automatic?

It should be only needed for once I think, because the incremental build should not remove them. But you may need to do it again if the content is changed.

I don't really want to do it manually because I often forget......

| For the annotation processor, looks like the PR mentioned above is very promising.

Looking forward to it.

then i try adding classPaths and see if it works:

  "java.test.config": [
    {
      "classPaths": [
        "${workspaceFolder}/src/main/resources/"
      ],
      "env": {
        "SPRING_PROFILES_ACTIVE": "test"
      },
      "workingDirectory": "${workspaceFolder}"
    }
  ],
jdneo commented 1 year ago

then i try adding classPaths and see if it works

Maybe not. That setting is only used when launching the tests. But code generation from annotation processors happens during the complation phase. Looks like those .sql files are required the generate the source code. otherwise, the classes under dao will not be generated from my observation.

What's the workflow when you running them in other IDEs?

uk-taniyama commented 1 year ago

| What's the workflow when you running them in other IDEs?

Just call gradle in Android Studio (IntelliJ IDEA).

16:07:03: Task execution finished ':test --tests "com.example.dao.MstEmployeeDaoTest.testSelectOne"'.
jdneo commented 1 year ago

I see. Because IDEA delegate the test execution to Gradle. And we are considering introducing that feature as well: https://github.com/microsoft/vscode-java-test/issues/1045

uk-taniyama commented 1 year ago

Thank you for your response. I look forward to seeing the finished product.

lmgillaspie commented 1 year ago

Is there any good way to make this place automatic?

It should be only needed for once I think, because the incremental build should not remove them. But you may need to do it again if the content is changed.

I don't really want to do it manually because I often forget......

| For the annotation processor, looks like the PR mentioned above is very promising.

Looking forward to it.

then i try adding classPaths and see if it works:

  "java.test.config": [
    {
      "classPaths": [
        "${workspaceFolder}/src/main/resources/"
      ],
      "env": {
        "SPRING_PROFILES_ACTIVE": "test"
      },
      "workingDirectory": "${workspaceFolder}"
    }
  ],

Thank you SO much for this configuration fix! We are having a similar issue where our test/resources/application.yml sets up our H2 in-memory database instead of the mysql setup in our main/resources/application.yml. The vscode gradle plugin and running in our terminal uses the correct application.yml but the test plugin does not....until we added the below config.

 "classPaths": [
         "${workspaceFolder}/src/test/resources/"
      ],

If you're trying to set the active profile, we did that in the tests with @ActiveProfiles("test"). Thank you!!

jdneo commented 1 month ago

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