Running one test while you write code is faster than running the whole build. ’s Java extra lets you run a JUnit test class, or a single test method, straight from the file you are editing. It uses 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 installs with the Java extra. It finds JUnit tests and builds the command that runs them.
  • 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:

@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 (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:

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

./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 · Next: Run configurations and Maven tasks in Neovim

Sources

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