How AI (Reinforcement Learning) Can Help Spring Developers Write Better Java Unit Tests
Overall, Java developers love Spring/Spring Boot because it saves them time and supports their testing experiences. The Diffblue Survey found that Spring’s standardized testing approach makes it easier to apply a technique from artificial intelligence (AI) called Reinforcement Learning to automate test-writing. Making this work for Java developers can slash development time as well as improve code coverage.
Australian developer Rod Johnson released the Spring Java framework under an open source software license in 2003 and in less than two decades it has become by far the most popular Java developer tool in the enterprise.
Our Diffblue 2021 Spring Framework User Survey released last month showed 86 percent of Java developers surveyed were users of Spring and that 96 percent of them benefited from using Spring. Indeed, through a set of questions never before asked in a survey, we discovered that using Spring also has a major positive impact on testing practices and code quality.
Overall, Java developers love Spring/Spring Boot because it saves them time and supports their testing experiences. Two benefits tied to testing stood out in our survey results:
- Spring Boot users report having better-tested code in their organization. They also spend 25 percent more time doing unit testing than Java developers who don’t use Spring. Developers all invest an inordinate amount of time writing tests, according to respondents, with no one spending less than approximately a fifth of their time on tests.
- AI-powered technology can dramatically speed up unit testing (up to 100X). Diffblue Cover, for example, is a free plugin that works well with Spring’s standardized way of doing unit testing and support for built-in mocking and ease of isolating test and database dependencies.
The Diffblue Survey found that Spring’s standardized testing approach makes it easier to apply a technique from artificial intelligence (AI) called Reinforcement Learning to automate test-writing. Making this work for Java developers can slash development time as well as improve code coverage.
The ideal application of reinforcement learning for Java would be to search for the best set of tests we can write that exercise a particular Java method, achieving the best line coverage but in a format that is as human readable as possible. Diffblue Cover does this with reinforcement learning.
The most famous example of reinforcement learning is Google AlphaGo, the program that beat several world Go Masters. It combines reinforcement learning with neural network approaches for its AI engine.
The secret to the AI engine behind Diffblue Cover is that, unlike AlphaGo, we don’t need a training dataset in the first place. We don’t need to predict how well a Java test will work. We can just run the test and observe how well it works. In test writing, we have the state of the program, and we can run the Java test we wrote against the method under test and see how well it performs. From there we can figure out how to alter/update the test for the next iteration. The IP behind reinforcement learning in Diffblue Cover’s AI engine is in how Cover computes the reward, constructs a human-readable test and determines the next action.
SEE ALSO: The last mile of sensitive data
By deploying reinforcement learning techniques in Diffblue Cover, we can automate unit test writing with low CPU and memory requirements compared to the massive computational effort required to train and run neural network models. Cover can run on a developer laptop with just 8Gb of memory and two Intel CPU cores. Neural network-powered image recognizers can require 10 Giga FLOPs (10 trillion floating point match operations) to identify an image.
Diffblue Cover uses AI techniques like reinforcement learning to automate the search for the best tests that we can find that exercise the most line coverage, while remaining readable by humans. We expect to see a lot more automation in writing key parts of software going forward. AI will play an important role. There just aren’t enough humans to do all the required coding anymore. Testing is only the start.