Neotest adapter for Java, using JUnit.
- ✅ Maven and Gradle projects
- ✅ Multimodule projects
- ✅ Debugging tests with
nvim-dap
Check ROADMAP.md to see what's coming!
- Make sure you have installed nvim-treesitter parsers. Use
:TSInstall java groovy
- Add neotest-java to your config:
LazyVim distro installation
return {
{
"rcasia/neotest-java",
ft = "java",
dependencies = {
"mfussenegger/nvim-dap", -- for the debugger
"rcarriga/nvim-dap-ui", -- recommended
"theHamsta/nvim-dap-virtual-text", -- recommended
},
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("neotest").run.run({ strategy = "dap" }) end, mode = "n" }
-- debug nearest test
keys[#keys + 1] = {"<leader>td", function() require("neotest").run.run({ vim.fn.expand("%"), strategy = "dap" }) end, mode = "n" }
end,
},
{
"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
},
},
},
},
}
lazy.nvim plugin manager
return {
{
"rcasia/neotest-java",
ft = "java",
dependencies = {
"mfussenegger/nvim-dap", -- for the debugger
"rcarriga/nvim-dap-ui", -- recommended
"theHamsta/nvim-dap-virtual-text", -- recommended
},
},
{
"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
},
},
},
},
}
- Run
:NeotestJava setup
Note
It will download the JUnit standalone jar from https://mvnrepository.com/artifact/org.junit.platform/junit-platform-console-standalone
{
junit_jar = nil, -- default: stdpath("data") .. /nvim/neotest-java/junit-platform-console-standalone-[version].jar
incremental_build = true
java_runtimes = {
-- there are no runtimes defined by default, if you wish to have neotest-java resolve them based on your environment define them here, one could also define environment variables with the same key/names i.e. `JAVA_HOME_8` or `JAVA_HOME_11` or `JAVA_HOME_17` etc in your zshenv or equivalent.
["JAVA_HOME_8"] = "/absolute/path/to/jdk8/home/directory",
["JAVA_HOME_11"] = "/absolute/path/to/jdk11/home/directory",
["JAVA_HOME_17"] = "/absolute/path/to/jdk17/home/directory",
},
}
Note that neotest-java would try it's best to determine the current project's runtime based on the currently running lsp servers, neotest-java supports both native neovim lsp and coc.nvim, it would try to fallback to your project configuration, supports both maven (reading from pom.xml) & gradle (reading from build.gradle or gradle.properties). In case the runtime is found but the location of it is not defined, neotest-java would prompt the user to input the absolute directory for the specific runtime version (only once).
Feel free to contribute to this project by creating issues for bug reports, feature requests, or suggestions.
You can also submit pull requests for any enhancements, bug fixes, or new features.
Your contributions are greatly appreciated. See CONTRIBUTING.md