Get the latest critical update information for Log4j vulnerability. See how to address the issue with Parasoft guidance. Learn More >>
Most development teams will agree that, although they don’t like it, unit testing is, in fact, extremely valuable. Despite the many tools that help with manual test creation, creating and maintaining unit tests still requires a lot of manual, time-consuming, often mind-numbing effort before adding business logic to a test. As such, despite having the intentions of great unit testing, development teams typically do the minimal amount of unit testing required, or skip it altogether.
A recent customer survey revealed, somewhat expectedly, that test creation, mocking/isolation, and maintenance are key issues standing in the way of unit testing success. Figure 1 shows the results of this survey, showing the top concerns developers are identifying with unit testing in Java.
Figure 1: Survey results show key areas of concern (percentage) that arise from unit testing.
To address these main areas of concern, we can turn to automated software testing. In this blog, I’ll show you how to use Parasoft Jtest to automate test execution and data collection, as well as automatically create unit tests, to save time and energy at test creation time. Parasoft Jtest’s automatic unit test creation technology sets up the test framework, instantiates objects, and configures mocks for appropriate objects and method calls used by the method under test. It creates regular JUnits, getting all the mundane work done so developers can focus on applying human intelligence and logic to their testing.
Unit testing is iterative, and in agile processes, unit testing has a good track record of improving project outcomes. And of course it is well-proven that testing earlier in the lifecycle is the best way to detect and remove expensive and time-consuming system-level bugs later on. More specifically, the following are some key benefits of unit testing:
Unfortunately, regardless of these benefits, developers are still struggling with unit testing, despite the desire to achieve better results. These struggles, also illustrated by the survey results above, include the following:
Software development teams must address these problems with test creation, isolation, and maintenance if they want to achieve the benefits of thorough unit testing, and the solution is automation. Parasoft Jtest’s automatic unit test creation tool provides the assistance needed for test creation and maintenance, while working within the developer’s IDE and leveraging existing test and mocking frameworks.
Creating unit tests is tedious and steers attention away from the more interesting parts of a project. The code itself is repetitive and often requires as much effort as the code under test, and on top of that, the unit test code itself requires fixing and debugging, as any code would. Fortunately, unit tests lend themselves well to automation, and automatic guidance from a unit testing tool can great simplify test creation, reduce the amount of debugging and fixing, and collect results and metrics to feed project analytics.
Many IDEs provide unit test creation wizards for JUnit, for example, but don’t provide “content” to complete the process. Assertions need to be manually defined, and if mocking frameworks are used, a significant amount of manual coding is required. Instead, Parasoft Jtest provides real-time, context-aware assistance in the developer’s IDE. With automatic unit test creation, the “content” missing from simple skeleton unit tests are quickly and efficiently completed, as you leverage your unit testing tool to perform the following:
To address this, let’s look at an example. Here, Parasoft Jtest’s Unit Test Assistant is invoked with an IDE to create parameterized tests:
The tool creates the following unit test code, at which point the developer completes the parameters as required:
To create test cases for different values of parameters, Parasoft Jtest’s Unit Test Assistant provides a test case wizard:
And you’re off.
Unit testing implies isolation of the object under test, which requires a fair amount of work if there are many dependencies. Even with mocking frameworks such as Mockito or PowerMock, there is still significant manual coding required. With an automated test assistant tool, you can detect dependencies and automatically fill in details required by the framework.
The tool itself analyzes the code under test, automatically detects dependencies, and makes recommendations to the developer.
For example, here we are executing the following unit test of class IWorkspaceClass:
Dependencies are detected during runtime, and the tool recommends mocking the dependencies that it has found:
Selecting “Mock It” with the tool generates the necessary mocking code with the unit test. Once this code is generated, it can be customized to represent correct logic as required. Automated detection of dependencies and subsequent mocking code creation greatly reduces the amount of manual, and potentially error-prone, coding required to mock objects.
Maintenance of test suites overlaps much of the work required for test creation, such as creating new tests, modifying tests to suit the underlying logic, mocking dependencies, test execution, and verification. Getting assistance from Parasoft Jtest’s Unit Test Assistant during test maintenance is just as valuable as during creation, as it provides updated feedback on runtime analysis results collected during test execution. For example, a new dependency in an object under test is detected at runtime and the tool prompts the developer with how to deal with it.
Equally critical during this phase is ensuring that the assertions are still valid. The Parasoft Jtest Unit Test Assistant provides recommendations that can detect changes in the code and update assertions to reflect new business logic.
Java developers that are already unit testing are likely using JUnit, and possibly an assertion framework for their project, such as Mockito or PowerMock. Test automation tools need to leverage these existing tools, since replacing an existing investment in unit testing would eliminate any cost and time benefit. Parasoft Jtest’s Unit Test Assistant integrates seamlessly with these existing tools, which is critical.
Unit testing has clear benefits, and although most development teams realize this, many are stymied by the effort of creating and maintaining tests. Using automated unit testing technologies from Parasoft Jtest will help users knock down these roadblocks, automating the mundane aspects of unit testing, including creation, isolation, mocking, and maintenance. To accelerate adoption, Parasoft Jtest leverages the development team’s existing investment in test and mocking frameworks, and gives back time to the developer while bringing quality back to the product.
Kapil is a Product Manager at Parasoft, focusing on Parasoft Jtest. Kapil held several technical positions ranging from software engineer to development lead, before moving into product management.