rcasia / neotest-java

Neotest adapter for Java.
MIT License
38 stars 21 forks source link

Neotest: No tests found #101

Closed Elyviere closed 2 months ago

Elyviere commented 4 months ago

Hi.

This is probably just a config error, but I haven't been able to figure it out.

I'm using the LazyVim distribution, and have tried to follow the setup for Neotest-java. All the setup works fine, :TSInstall java and :NeoTestJava setup both run without issues. However, when I try to run a test using <leader>tt I get a "Neotest: No tests found" notification, despite my cursor literally being on the test. The LazyVim java extra, ["<leader>tt"] = { require("jdtls.dap").test_class, "Run All Test" }, version does work and runs the test correctly, so it's specifically the require("neotest")... version that is unable to find and run the test.

image

I've tried this in as simple a scenario as I could think of, a basic Maven project with a Main.java file printing "HelloWorld!", and a test verifying that output. I've also checked to ensure TreeSitter is being correctly loaded, and it is indeed being loaded as a dependency of neotest, as seen here: image

My config is a straight up copy-paste from the README.md, placed in lua/plugins/neotest.lua. I also tried placing the config in the suggested location, but it didn't fix the issue and I probably want the default configuration anyway so I removed it again.

return {
  {
    "rcasia/neotest-java",
    init = function()
      -- override the default keymaps.
      -- needed until neotest-java is integrated in LazyVim
      local keys = require("lazyvim.plugins.lsp.keymaps").get()
      -- run test file
      keys[#keys + 1] = {"<leader>tt", function() require("neotest").run.run(vim.fn.expand("%")) end, mode = "n" }
      -- run nearest test
      keys[#keys + 1] = {"<leader>tr", function() require("neotest").run.run() end, mode = "n" }
      -- debug test file
      keys[#keys + 1] = {"<leader>tD", function() require("jdtls.dap").test_class() end, mode = "n" }
      -- debug nearest test
      keys[#keys + 1] = {"<leader>td", function() require("jdtls.dap").test_nearest_method() end, mode = "n" }
    end,
  },
  {
    "nvim-neotest/neotest",
    dependencies = {
      "nvim-neotest/nvim-nio",
      "nvim-lua/plenary.nvim",
      "antoinemadec/FixCursorHold.nvim",
      "nvim-treesitter/nvim-treesitter"
    },
    adapters = {
        ["neotest-java"] = {
          -- config here
        },
    },
  },
}
rcasia commented 4 months ago

How does your project structure look? When you open the test summary, does it show "neotest-java" on top?

Elyviere commented 4 months ago

It follows the maven standard (see screenshot). The test summary (<leader>ts) only says "No tests found".

image

Elyviere commented 4 months ago

:checkhealth noted an error, so I think I'm closer to a solution now image

Wrapping adapters in an opts object makes the error in checkhealth go away and the test show up in the summary window: image

  {
    "nvim-neotest/neotest",
    dependencies = {
      "nvim-neotest/nvim-nio",
      "nvim-lua/plenary.nvim",
      "antoinemadec/FixCursorHold.nvim",
      "nvim-treesitter/nvim-treesitter"
    },
    opts = {
      adapters = {
        ["neotest-java"] = {
          -- config here
        },
      },
    },
  },

However, when running the test I now instead get the following error:

   Warn  19:22:07 notify.warn Neotest neotest-java: ...share/nvim/lazy/neotest/lua/neotest/lib/xml/internal.lua:92: XMLDecl not at start of document [char=1]

stack traceback:
    ...ocal/share/nvim/lazy/neotest/lua/neotest/client/init.lua:89: in function <...ocal/share/nvim/lazy/neotest/lua/neotest/client/init.lua:88>
    [C]: in function 'error'
    ...share/nvim/lazy/neotest/lua/neotest/lib/xml/internal.lua:92: in function 'errorHandler'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/xml/parser.lua:132: in function 'err'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/xml/parser.lua:192: in function 'parseXmlDeclaration'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/xml/parser.lua:345: in function 'parseTagType'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/xml/parser.lua:427: in function 'parse'
    ...cal/share/nvim/lazy/neotest/lua/neotest/lib/xml/init.lua:18: in function 'parse'
    ...lazy/neotest-java/lua/neotest-java/util/read_xml_tag.lua:12: in function 'f'
    .../nvim/lazy/neotest/lua/neotest/lib/func_util/memoize.lua:56: in function 'read_xml_tag'
    .../lazy/neotest-java/lua/neotest-java/build_tool/maven.lua:13: in function 'source_directory'
    .../lazy/neotest-java/lua/neotest-java/build_tool/maven.lua:38: in function 'get_sources'
    ...-java/lua/neotest-java/command/junit_command_builder.lua:117: in function 'build'
    ...lazy/neotest-java/lua/neotest-java/core/spec_builder.lua:45: in function 'build_spec'
    ...al/share/nvim/lazy/neotest/lua/neotest/client/runner.lua:76: in function '_run_tree'
    ...al/share/nvim/lazy/neotest/lua/neotest/client/runner.lua:65: in function <...al/share/nvim/lazy/neotest/lua/neotest/client/runner.lua:22>
    [C]: in function 'xpcall'
    ...ocal/share/nvim/lazy/neotest/lua/neotest/client/init.lua:84: in function 'run_tree'
    ...al/share/nvim/lazy/neotest/lua/neotest/consumers/run.lua:73: in function 'func'
    ...yviere/.local/share/nvim/lazy/nvim-nio/lua/nio/tasks.lua:170: in function <.../.local/share/nvim/lazy/nvim-nio/lua/nio/tasks.lua:169>
rcasia commented 4 months ago

I found this on stackoverflow: https://stackoverflow.com/questions/42867268/luaxml-xmldecl-not-at-start-document-char-1

This error comes from neotest, that is using xml2lua.

The file pom.xml file seems to have a BOM/charset problem.

You could see the file encoding with:

file -i pom.xml
# > pom.xml: text/xml; charset=utf-8
# or
# > pom.xml: text/xml; charset=us-ascii

Eitherway it would be nice to control this library error in this test adapter.

Elyviere commented 4 months ago

I tried the suggestions in the stackoverflow, but file -i pom.xml returns us-ascii and Notepad++ shows the file to be UTF-8 encoded without BOM, so that does not appear to be my problem. I also verified my other files in the project, all of which are us-ascii encoded. Sounds like I might want to take this part of the issue to Neovim directly.

My previous comment regarding the LazyVim config should be applied to the Readme for Neovim-java though I think.

Elyviere commented 2 months ago

In case anyone comes looking here in the future, I never solved this issue.