Published on: April 10, 2025
5 min read
Learn step-by-step how to add unit tests to a Java application using agentic AI (includes a video tutorial).
Assuring the quality of your applications via test-driven design, good code coverage, and issue detection is critically important to your customers and your reputation, but it can also be a time-consuming endeavor. GitLab Duo Workflow, agentic AI built on top of the most comprehensive DevSecOps platform, can help you quickly complete development tasks such as adding unit tests to a Java application. This tutorial demonstrates how by using this sample Java project.
GitLab Duo Workflow is currently in private beta. Join the waitlist to see what’s possible with AI agents that understand your entire SDLC.
Open the Java project in Visual Studio Code (after cloning it to your local machine). Ensure that you’re in a feature branch (not the main or default branch) before you start. If you’re already working on a merge request, it will have its own associated feature branch.
(This step is optional.) Navigate to the file that defines the Java class for which you’d like to have GitLab Duo Workflow create unit tests. Inspect it so that you can later confirm that the generated unit tests do cover its class members. This is what you would see:
Note: We are assuming that you already enabled the GitLab Duo Workflow extension in your VS Code. If not, please refer to the setup documentation.
Create unit tests for class defined in the Product.java file and store the unit tests in its own file titled ProductTest.java
Review the analysis and plan and, if you are satisfied with them, click Approve plan at the bottom of the window.
GitLab Duo Workflow will start executing the approved plan and making modifications to your project accordingly.
Once the execution of the plan is finished, you will see a new directory src/test/java/csaa/jspring/ProductManager
in the project with a new file in it named ProductTest.java
, which contains all the unit tests for the Product.java
class.
ProductTest.java
and you will see that it has some import statements underlined in red indicating some import errors:Let’s have GitLab Duo Workflow fix these for us.
Note: We could have also asked GitLab Duo Workflow in our first prompt to update the pom.xml
file accordingly. But since we didn’t, let’s fix these errors in a new workflow.
The file ProductTest.java has an error “The import org.junit cannot be resolved”. Please fix it
pom.xml
file. It then edits it and removes the outdated JUnit dependency, and follows that with the addition of the correct dependency and version for JUnit. Lastly, it reads the ProductTest.java
file to clear all the dependency errors.Through the execution of this plan, GitLab Duo Workflow is effectively making updates to the project to achieve what was requested in the prompt, saving time and effort, and increasing productivity so that developers can spend more time innovating and creating value for their organization.
If you’d like to see what you read above in action, watch the following video:
Sign up for the GitLab Duo Workflow private beta waitlist to see what’s possible with AI agents that understand your entire SDLC.