Features

Spring Testing Improvements in IntelliJ IDEA 2017.1

In the last post we talked about Spring Data improvements, but it doesn’t stop there. IntelliJ IDEA 2017.1, released last week, has enhanced support for testing using the Spring framework.

Gutter Icons and Navigation

You’ll see that where you use the standard test annotations, you have gutter icons to help you to navigate around the code, the same as you’d usually expect from Spring annotations.  For example, you can see and navigate to the relevant application contexts, and navigate to the declarations of autowired beans.

Gutter icons for tests

And of course IntelliJ IDEA recognises classes annotated with Spring test annotations as a test that can be run.

Run test

These screenshots show the functionality working for @RestClientTest, but it applies equally to @JsonTest, @DataJpaTest, @WebMvcTest, @WebIntegrationTest, and so on, including your own custom tests.

Custom test annotations

IntelliJ IDEA also understand mocks, and how they work in Spring tests.  Gutter icons take you to the declaration of the mock.

Mocks

Code Completion

You can easily work with WebAppConfiguration with the provided navigation and code completion.

WebAppConfiguration

There’s also support for test property sources, and meta annotations for properties. For example, you get code completion for @TestPropertySource and navigation to the properties file.

TestPropertySource

Even if you declare your own TestProperties annotation and alias the TestPropertySource attributes, code completion and navigation still works as expected.

TestProperties

Another place where completion and navigation are provided is in the @Sql annotation.  Here, IntelliJ IDEA offers suggestions, and can even show you when you’ve configured something incorrectly.

Sql

Inspections

Inspections have been added to help catch problems that arise from using the wrong attributes on @DirtiesContext.  For example, if you set the methodMode at the class level, you’ll see a warning.

Method level on class

Similarly for setting hierarchy mode when it’s not applicable

Hierarchy

And also for using classMode at the method level

Class at method level

More new inspections show where the method signature doesn’t match the annotation’s expectations. For example, if you have a return type on a method annotated with either @BeforeTransation or @AfterTransaction.

Wrong return type

Or if your @BeforeTransation or @AfterTransaction methods take arguments.

Wrong arguments

In summary, IntelliJ IDEA 2017.1 makes testing Spring applications even easier, with inspections, code completion, and easy navigation.

Develop with Pleasure!

image description

Discover more