Start United States USA — software Start to Love Spring Testing With the Unit Test Assistant for Java

Start to Love Spring Testing With the Unit Test Assistant for Java

347
0
TEILEN

As challenging as unit tests can be, the Unit Test Assistance stands to simplify them. See how it works with Java code in a Spring Boot project.
The Spring framework (along with Spring Boot) is one of the most popular Java enterprise software frameworks. Its use in mission-critical applications means it has come under scrutiny for quality and security. In a previous post, we discussed how developers don’t like unit testing despite its proven track record of improvement, and detailed how Parasoft’s Unit Test Assistant can provide a guided and automated approach to testing to make testing not only more palatable, but also easier and more efficient. In this post, I’ll continue the same theme with the Spring framework, showing you how automated and guided testing can be leveraged in this important application framework. From here on out, I’ll refer to the Unit Test Assistant by its acronymn, UTA.
The Spring framework comes with nice support for integration testing, but a lot of manual coding is required to set up test cases properly. Building and maintaining tests for Spring applications presents developers with a unique set of challenges, including the following:
These challenges, combined with the fact that writing comprehensive and maintainable test suites takes a lot of time in general, result in developers not writing enough tests. In turn, this leads to security vulnerabilities, defects, regressions, and lots of headaches.
UTA helps by making the process of generating, improving, and maintaining JUnit tests far easier and less time-consuming, so that developers build good tests quickly and get back to what they presumably love – writing code.
The Spring Framework includes a testing framework that makes testing Controllers, Services, and other components much easier. It includes functionality for configuring the Spring test container, invoking Controller handler methods, and validating behavior with custom assertions.
An example Spring MVC Controller:
An example Spring MVC Junit test may look like this:
What’s wrong with the above test? Nothing really – but imagine a more complex controller method, with multiple handler methods that accept more arguments and produce more outputs. Writing the tests would take a lot more time, especially if good testing coverage is important. In addition, most real tests require significantly more configuration (XML or class configurations, sessions and environments, security, etc).
The Unit Test Assistant helps developers write Spring tests in multiple ways:
Generating Spring tests in UTA is straightforward – simply select a Spring handler method in your IDE for your controller and choose a test-creation action:
UTA supports test generation using XML or class configuration by setting the “ContextConfiguration attributes for Spring tests” option in preferences.
(If you want more info about mocking dependencies in Spring tests, I’ll adress it in a future post.)
Since Spring Boot provides simplified configuration for Beans, as well as additional annotations for tests, UTA generates slightly different tests when it detects Spring Boot in your project. For example, MockMvc is autowired, dependencies are mocked with @MockBean, and the @SpringBootTest annotation is used.
You can run generated tests using any normal JUnit runner. UTA provides toolbar actions which run the Junit and analyze the test.
After tests are run, the test execution flow is shown, and recommendations for improving the test are made by UTA and reported in your IDE:
Handler methods are often configured to accept path, query, or other parameters as arguments to the method. To test the MVC handler method, you can use MockMvc to build the path/query and any other parameters needed to invoke the method.
UTA auto-configures the mockMvc.perform call to invoke the handler method. Individual parameters show up in the test as local variables (or parameters in a parameterized test) which need to be configured for the test to run properly.
For example:
Here, the “id” String needs to be configured – if not, then the path used would be “/people/”, and Spring will not match the provided path to the appropriate handler method.
UTA looks for various types of handler method parameters and automatically prepares the test for them in the following ways:
Running a test which does not cause the handler method to be invoked produces a recommendation like the following:
Depending on what the handler method is supposed to provide to callers, it may return a variety of types. Most of the time, handler methods return either a ModelAndView (or similar objects like Model or RedirectView) to serve a page, or a ResponseEntity of some kind (sometimes just the raw object to be serialized). This response is accessible to the Spring MVC Test framework for validation.
For example, the following assertions were added by UTA for a handler method which returns a ModelAndView:
Once the test is generated, you can uncomment these assertions and populate values to quickly build a useful and valuable test. If an assertion fails at runtime, UTA provides a recommendation and quick-fix to automatically update the expected value or simply remove the assertion. To quickly set up the assertion with a proper value, you can uncomment an assertion, let it fail, and then use a quick-fix to set the correct expected value.
Spring (combined with Spring Boot) is the leading enterprise Java application framework and as such needs an appropriate level of testing to ensure the quality and security of applications built with it. But unfortunately, this level of testing is not being achieved currently, mostly due to a lack of time and the amount of manual coding and maintenance required. The Java Unit Test Assistant provides not just unit test automation but guided test creation and dependency management, to accelerate test creation and reduce maintenance.

Continue reading...