Streamlining Unit Testing with GitHub Copilot: A Developer’s Guide – Spring Boot Java and Golang Application
Wed, 24 Apr 2024 15:51:17 -0000
|Read Time: 0 minutes
Unit testing is the backbone of robust software development. It ensures that the individual components of your codebase function correctly in isolation. However, writing comprehensive unit tests can be a labor-intensive task, especially when dealing with complex logic or large codebases. Enter GitHub Copilot – an AI-powered tool that promises to revolutionize the way developers write code.
The scenario
Imagine you’re building a Spring Boot or Golang based API. You’ve created a CustomerService class that contains methods to perform various business logic operations related to customers. Now, let’s see how GitHub Copilot can help generate unit tests and accelerate the process of writing unit tests for this class. GitHub Copilot aims to boost productivity by reducing boilerplate code and allowing developers to focus on business logic.
Spring Boot example
We’ll begin by creating the CustomerController class, which currently lacks any content. By effectively prompting Copilot, we can generate two essential operations: createCustomer and getCustomer.
Figure 1. Copilot prompt for creating a controller class with two operations
Note: The input to the prompt should be effective with all the necessary details.
Based on the prompt inputs, GitHub Copilot generated a class CustomerController with two methods: createCustomer and getCustomer.
Figure 2. Copilot auto generated code for a controller and two operations
Now that the CustomerController class has the necessary functions, the next critical step is to create unit tests to validate their functionality. Initially, the CustomerControllerTest file is empty. We’ll use effective prompting techniques to assist GitHub Copilot in generating unit tests for both the createCustomer and getCustomer functions.
Figure 3. Copilot prompt for writing unit test for Controller class
Upon generating the test function CustomerControllerTest using the GitHub Copilot command, you are now presented with a decision to accept or discard the generated test functions. Your task involves a thorough review, allowing you to make an informed choice regarding their acceptance or rejection.
Figure 4. Copilot prompt for accepting the auto generated unit tests
Upon acceptance, all generated contents are added to CustomerControllerTest class.
Figure 5. Copilot auto generated unit test cases
In the CustomerControllerTest function, errors such as import errors may arise. Utilize GitHub Copilot’s /fix feature to address and rectify these errors as they appear.
Figure 6. Copilot /fix prompt to fix errors in the code
Enhance your understanding of your code using the /explain feature. Simply select the code snippet you wish to understand better and employ the explain feature. This functionality provides developers with deeper insights into their codebase. Additionally, GitHub Copilot will provide suggestions such as ‘missing pom dependency’ to aid in code improvement, as seen in the following example.
Golang examples
Now, let’s explore how we can leverage similar features when developing a Go application.
Main.go currently stands as an empty file awaiting the creation of a router for two essential operations: creating and retrieving customers. Ensure all necessary details are included within the comments, following the format demonstrated in the following figure.
Figure 7. Copilot prompt for creating a router for Get and Create customer operations in GO
Note: The input to the prompt should be effective with all the necessary details.
Based on the prompt inputs, GitHub Copilot generated router code with two endpoints: createCustomer and getCustomers.
Figure 8. Copilot auto generated code for router and Get and Create customer endpoints
Next, we must develop handlers for both creating and retrieving customers. Currently, the CustomerHandler file remains void of any content. Let’s initiate a prompt to generate the necessary operations, as shown in the following figure.
Figure 9. Copilot prompt for writing a customer handler for Get and Create customer operations
Note: Please ensure that the input provided to the prompt is comprehensive and includes all necessary details for effective results.
According to the provided prompt inputs, GitHub Copilot has generated a class named CustomerHandler containing two methods: createCustomer and getCustomers.
Figure 10. Copilot auto generated code for Get and Create customer operation
The next step involves creating unit tests. Currently, CustomerHandler_test remains empty. GitHub Copilot will generate the unit tests for both creating and retrieving customers using the prompt shown in the following figure.
Figure 11. Copilot prompt for writing unit test
Now that the CustomerHandler_test class has been generated, as displayed in the following figure, GitHub Copilot provides you with the option to either accept or discard these tests. Take the time to review them thoroughly and make your decision accordingly.
Figure 12. Copilot auto generated unit test cases
Conclusion
Writing a REST Controller
Without Copilot: Writing a Customer Controller from scratch might take a developer an hour, depending on their familiarity with Spring Boot and the complexity of the controller.
With Copilot: By providing suggestions and auto-completion for code snippets, Copilot has the potential to cut development time by around 20-40%.
For example, it typically takes 1 hour to write the Customer Controller without Copilot. With Copilot's assistance, it might take around 20-30 minutes, saving approximately 30-40 minutes.
Writing unit test cases
GitHub Copilot can significantly reduce the time required to write unit tests by providing suggestions and auto-completion for test cases based on the provided prompt.
With Copilot's assistance, the time required to write unit tests for CustomerControllerTest was reduce by around 20-40%, dependent on the complexity of the controller and the level of test coverage desired. For example, it typically takes 2 hours to write unit tests without Copilot's assistance. With Copilot's help, it might take around 40 min to 1 hour, saving approximately 1 to 1.2 hours.
In conclusion, GitHub Copilot is a valuable tool for developing a Customer Service basic CRUD operation and its unit tests for createcustomer and getcustomer. Copilot significantly reduces the time and effort required for coding tasks, resulting in enhanced productivity and efficiency. Moreover, the reduction in lines of code ensures cleaner, more maintainable codebases while accelerating sample customer service development.
Happy writing, DEVs!
Authors:
Manoj Kumar Manoj Kumar | LinkedIn
Ratnesh Yadav Ratnesh (रत्नेश) Yadav | LinkedIn