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

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

Closed
uk-taniyama opened this issue Nov 21, 2022 · 15 comments
Closed
Labels

Comments

@uk-taniyama
Copy link

  • Java Debugger extension version:v0.46.0

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
Copy link
Author

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
Copy link
Member

jdneo commented Nov 22, 2022

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
Copy link
Author

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
Copy link
Member

jdneo commented Nov 23, 2022

We are actually working on the annotation processor support for the Gradle project: eclipse-jdtls/eclipse.jdt.ls#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
Copy link
Author

We are actually working on the annotation processor support for the Gradle project: eclipse-jdtls/eclipse.jdt.ls#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
Copy link
Member

jdneo commented Nov 23, 2022

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
Copy link
Author

uk-taniyama commented Nov 23, 2022

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
Copy link
Member

jdneo commented Nov 23, 2022

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
Copy link
Author

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
Copy link
Member

jdneo commented Nov 23, 2022

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
Copy link
Author

| 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
Copy link
Member

jdneo commented Nov 23, 2022

I see. Because IDEA delegate the test execution to Gradle. And we are considering introducing that feature as well: #1045

@uk-taniyama
Copy link
Author

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

@lmgillaspie
Copy link

lmgillaspie commented Jan 12, 2023

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
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

3 participants