---
title: "Running Java Tests in Neovim"
canonical: "https://mnafshin.info/devtools/08-running-java-tests/"
author: "Afshin Amini"
date: 2026-11-29
---
# Running Java Tests in Neovim

Running one test while you write code is faster than running the whole build. LazyVim's Java extra lets you run a JUnit test class, or a single test method, straight from the file you are editing. It uses jdtls together with `java-test`, the same test extension the Java support in VS Code uses, and runs the test through the debugger from Part 7.

## How tests run

Three pieces work together:

- **jdtls** knows which classes and methods are tests, because it compiles them.
- **java-test** is a jdtls extension that Mason installs with the Java extra. It finds JUnit tests and builds the command that runs them.
- **nvim-dap** starts the test JVM, the same way it starts an app for debugging.

You need `dap.core` enabled, as in Part 1. The Java extra only adds the test keys when the debugger and `java-test` are installed.

## The test class we run

Module 9 of the companion code has one test class, `CatalogTest`. It is a `@SpringBootTest` that starts the whole application with an in-memory database and calls the `/catalog` endpoint through `MockMvc`. One of its six tests checks the second page:

```java
@Test
void secondPageOfTwoHoldsCroissantAndEspressoBeans() throws Exception {
    this.mvc.perform(get("/catalog").param("page", "1").param("size", "2"))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.content[0].name").value("Croissant"))
            .andExpect(jsonPath("$.content[1].name").value("Espresso beans"));
}
```

[CatalogTest.java](https://github.com/mnafshin/springuniverse-code/blob/main/module-09-catalog/src/test/java/info/mnafshin/springuniverse/shop/CatalogTest.java) (shortened)

The request asks for page 1 with two items per page. Because pages count from zero, that is the third and fourth product.

## Run the whole class

Open `CatalogTest.java` and press `<leader>tt`, "run all tests" of the class. jdtls compiles what changed, Spring Boot starts once for the class, and all six tests run.

When they finish, a notification says *Tests finished. Results printed to dap-repl.* with a count, *6 succeeded* in our run. Open the debug console with `<leader>dr` to see each test by name with a check mark:

```text
✓  secondPageOfTwoHoldsCroissantAndEspressoBeans
✓  seedsThreeCategoriesAndFourProducts
✓  listsEveryProductWithItsCategory
```

A `@SpringBootTest` starts a full application context, so expect the run to take noticeably longer than a plain unit test.

## Where the classpath comes from

jdtls runs the test with the classpath it imported from `pom.xml`: the module's own classes, its test resources, and dependencies such as H2 and `spring-boot-starter-test`. If you add a test dependency to the POM, run `:JdtUpdateConfig` first (Part 2), or the test will not see it.

## Run one test

Put the cursor inside `secondPageOfTwoHoldsCroissantAndEspressoBeans` and press `<leader>tr`, "run nearest test". Only that method runs, and the console shows one check mark. This is the key you use most while writing a test: change it, press `<leader>tr`, read the result.

LazyVim also maps `<leader>tT` to choose a test from a list; we used the first two keys in our test.

## When a test fails

A failing run looks the same, with a cross instead of a check. In one of our runs the class failed to compile because of a leftover file, and the notification read *Tests finished. Results printed to dap-repl. (✗ 6 / 6)*. The debug console listed each test with `✗`, followed by its exception and the `Caused by` chain, down to *Unresolved compilation problems*. Read the console from the first failure; later failures in a `@SpringBootTest` often just repeat that the application context could not start.

nvim-jdtls also fills the quickfix list from test failures when the stack trace points into your project files. In our failed run the cause was a compile error, not an assertion in a test, so that list stayed empty, and the console was the place to look.

## Running tests with the build tool

The keys above run tests through jdtls, which is quick for one class. To run the whole module the way your CI does, use Maven. Part 9 saves that as a task you can start from Neovim:

```sh
./mvnw -pl module-09-catalog test
```

This also catches what jdtls cannot see, such as a Maven plugin that fails the build.

## Compared with IntelliJ IDEA

IntelliJ shows a test tree with green and red bars, rerun buttons and a history of runs. Here results are lines in the debug console. There is a separate plugin, neotest with a Java adapter, that adds a test tree to Neovim; LazyVim's Java extra does not use it, and we did not test it for this series.

## Recap

- The Java extra runs JUnit tests through jdtls, the `java-test` extension and nvim-dap; enable `dap.core`.
- `<leader>tt` runs the test class, `<leader>tr` the test under the cursor and `<leader>tT` lets you choose one.
- A notification gives the count; `<leader>dr` shows each test in the debug console.
- Failures appear in the debug console with `✗`, the exception and its causes.
- Run the whole module with Maven, for example as an overseer task, before you push.

**Series: Neovim as a Java IDE.** Previous: [Debugging a Spring Boot app in Neovim](/devtools/07-debugging-spring-boot/) · Next: [Run configurations and Maven tasks in Neovim](/devtools/09-run-configurations-and-maven-tasks/)

## Sources

Official documentation and well-known tutorials read while writing this article. The wording here is our own.

- [LazyVim: Java extra (test keymaps)](https://www.lazyvim.org/extras/lang/java)
- [nvim-jdtls README: debugging JUnit tests](https://github.com/mfussenegger/nvim-jdtls#nvim-dap-configuration)
- [vscode-java-test (the java-test extension)](https://github.com/microsoft/vscode-java-test)
- [Spring Boot reference: Testing](https://docs.spring.io/spring-boot/reference/testing/index.html)
- [IntelliJ IDEA: Run tests](https://www.jetbrains.com/help/idea/performing-tests.html)
- [Lorenzo Bettini: Neovim and Java with LazyVim, part 4: Running programs](https://www.lorenzobettini.it/2025/03/neovim-and-java-with-lazyvim-part-4-running-programs/)
- [Nirmal Ravidas: Setting Up a Full Spring Boot Dev Environment on Linux with Neovim and LazyVim](https://nirmalravidas.hashnode.dev/spring-boot-dev-environment-linux-neovim-lazyvim)
- [Dhruva Sagar: Neovim as a Java IDE](https://dhruvasagar.dev/posts/neovim-java-ide/)
