Neotest adapter for Java.
neotest-java
Neotest adapter for Java, using JUnit.
โญ Features
- โ Maven & Gradle support (Groovy & Kotlin DSL)
- โ Multi-module projects with automatic detection
- โ
JUnit 5 (Jupiter):
@Test,@ParameterizedTest,
@TestFactory, nested tests
- โ JUnit Platform 1.10.x & 6.x
- โ
Spring Framework: auto-loads
application.yml,
application-test.yml, .properties
- โ Debugging with nvim-dap (breakpoints, JDWP, DAP REPL)
- โ Incremental compilation via nvim-jdtls
- โ Automatic classpath management from LSP
- โ JUnit JAR management with version detection
- โ
Health check via
:checkhealth neotest-java
Check ROADMAP.md to see what's coming!
๐ฆ Installation
Prerequisites
- Neovim 0.10.4+
- nvim-treesitter with Java parser:
:TSInstall java - A JDTLS-based Java LSP โ either
- nvim-dap - For debugging support (optional)
Setup with lazy.nvim
Using nvim-jdtls
return {
{
"rcasia/neotest-java",
ft = "java",
dependencies = {
"mfussenegger/nvim-jdtls",
"mfussenegger/nvim-dap", -- for debugging (optional)
"rcarriga/nvim-dap-ui", -- recommended
"theHamsta/nvim-dap-virtual-text", -- recommended
},
},
{
"nvim-neotest/neotest",
dependencies = {
"nvim-neotest/nvim-nio",
"nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter",
},
config = function()
require("neotest").setup({
adapters = {
require("neotest-java")({
-- Optional configuration here
}),
},
})
end,
},
}
Using nvim-java
nvim-java is fully compatible โ neotest-java communicates with the LSP through the standard vim.lsp.Client API and does not depend directly on nvim-jdtls.
return {
{
"rcasia/neotest-java",
ft = "java",
dependencies = {
"mfussenegger/nvim-dap", -- for debugging (optional)
},
},
-- nvim-java handles JDTLS setup separately
{ "nvim-java/nvim-java" },
{
"nvim-neotest/neotest",
dependencies = {
"nvim-neotest/nvim-nio",
"nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter",
},
config = function()
require("neotest").setup({
adapters = {
require("neotest-java")({
-- Optional configuration here
}),
},
})
end,
},
}
JUnit JAR Installation
After setting up the plugin, run:
:NeotestJava setup
This will automatically download and verify the JUnit Platform Console Standalone JAR from Maven Central with SHA-256 checksum verification.
The plugin will detect if you have an older JUnit version
installed and prompt you to upgrade.
โ๏ธ Configuration
All configuration options are optional. Pass them to require("neotest-java")({}):
require("neotest").setup({
adapters = {
require("neotest-java")({
junit_jar = nil, -- default: auto-detected
jvm_args = { "-Xmx512m" }, -- custom JVM arguments
incremental_build = true, -- recompile changed files
disableupdatenotifications = false,
testclassnamepatterns = {
"^.Tests?$", "^.IT$", "^.*Spec$"
},
}),
},
})
Options
junit_jar(string?) โ default:
stdpath("data")/neotest-java/junit-*.jar
Path to JUnit Platform Console Standalone JAR
jvm_args(string[]) โ default:{}
incremental_build(boolean) โ default:true
disableupdatenotifications(boolean) โ
false
Disable JUnit update notifications
testclassnamepatterns(string[]) โ
{"^.Tests?$", "^.IT$", "^.*Spec$"}
Regex patterns for test class names
๐ง Advanced: Dependency Injection API
neotest-java exposes a public dependency injection API that allows you to override core adapter components. This is useful for:
- Using custom LSP clients (e.g., coc.nvim)
- Custom classpath resolution (e.g., Bazel)
- Custom build tools (e.g., Ant, Bazel)
- Custom compilation strategies
- Testing downstream plugins/configs
Usage
Pass a second argument to the adapter constructor with your overrides:
require("neotest").setup({
adapters = {
require("neotest-java")({
-- configuration options
}, {
-- dependency overrides (all optional)
clientprovider = mycustomclientprovider,
classpathprovider = mycustomclasspathprovider,
binaries = mycustombinaries,
lspcompiler = mycustom_compiler,
buildtoolgetter = mycustombuildtoolgetter,
methodidresolver = mycustommethodidresolver,
}),
},
})
Example: Using coc.nvim as LSP client
local customclientprovider = function(cwd)
-- Get coc.nvim's Java client
local clients = vim.lsp.get_clients({ name = "coc" })
for _, client in ipairs(clients) do
local fts = client.config.filetypes or {}
if vim.tbl_contains(fts, "java") then
return client
end
end
error("No coc.nvim Java client found")
end
require("neotest").setup({ adapters = { require("neotest-java")({}, { clientprovider = customclient_provider, }), }, })
Example: Custom classpath resolution
local customclasspathprovider = {
getclasspath = function(basedir, additional_entries)
-- Your custom classpath resolution logic
local result = vim.system({
"bazel", "query", "classpath",
tostring(base_dir)
}):wait()
return result.stdout
end,
}
require("neotest").setup({ adapters = { require("neotest-java")({}, { classpathprovider = customclasspath_provider, }), }, })
Type Reference
See the neotest-java.Dependencies type annotation in lua/neotest-java/init.lua for the full API reference with type signatures.
โ ๏ธ Troubleshooting
Multi-module: "URI does not belong to any Java project"
Tests in one module pass but tests in another fail with:
Error -32001: Given URI does not belong to any
Java project.
Cause: eclipse.jdt.ls (jdtls) is started once per module instead of once per workspace. When neotest-java runs tests for module B, it talks to module A's jdtls instance, which rejects URIs it doesn't own.
This happens when pom.xml or build.gradle is used as a root marker in the jdtls configuration. Because every module directory contains its own build file, jdtls resolves root_dir to the nearest module root rather than the repository root.
Solution: Remove pom.xml and build.gradle from the root_dir markers and keep only the repo-level markers (.git, mvnw, gradlew).
With nvim-jdtls (ftplugin/java.lua style):
-- Before (broken for multimodule):
rootdir = require("jdtls.setup").findroot({
".git", "mvnw", "gradlew", "pom.xml"
})
-- After (correct): rootdir = require("jdtls.setup").findroot({ ".git", "mvnw", "gradlew" })
With the newer vim.lsp.config / vim.fs.root API (Neovim 0.11+):
-- Before (broken for multimodule):
root_dir = vim.fs.root(0, {
".git", "mvnw", "gradlew", "pom.xml"
})
-- After (correct): root_dir = vim.fs.root(0, { ".git", "mvnw", "gradlew" })
With this change a single jdtls instance starts at the repository root and handles all modules. eclipse.jdt.ls natively understands Maven and Gradle multimodule projects, so no further configuration is needed.
The first time you open a Java file after this change,
jdtls will reindex the whole workspace from a new
-data directory. This can take a couple of minutes
for large projects.
Spring Tests: "parameter name information not available"
If you're running Spring tests that use reflection (e.g., @MockBean, @WebMvcTest) and encounter errors like:
java.lang.IllegalArgumentException: Name for argument
of type [int] not specified, and parameter name
information not available via reflection.
Ensure that the compiler uses the '-parameters' flag.
Solution: Configure the JDTLS compiler to preserve parameter names in bytecode by adding the following to your project's .settings/org.eclipse.jdt.core.prefs file:
org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
If the .settings directory doesn't exist, create it in your project root:
mkdir -p .settings
echo "org.eclipse.jdt.core.compiler.codegen.methodParameters=generate" \
> .settings/org.eclipse.jdt.core.prefs
After adding this setting, restart your LSP server (:LspRestart) and run your tests again.
๐ค Contributing
Contributions are welcome! Please feel free to:
- ๐ Report bugs and issues
- ๐ก Suggest new features or improvements
- ๐ง Submit pull requests
โจ Acknowledgements
Thanks to all contributors who have helped improve this project!