Activity ‐ Generating Java Unit Tests - accentient/github-copilot-devs GitHub Wiki

Generating Java Unit Tests with GitHub Copilot

Objective

This activity focuses on testing and debugging practices while building a Unit Converter application in Java with GitHub Copilot. Participants will explore how GitHub Copilot assists in generating unit tests, handling boundary conditions, and debugging issues efficiently. By the end of the activity, participants will:

  • Understand how to integrate and use GitHub Copilot for testing and debugging in Java
  • Experience generating and running unit tests for various conversion methods with GitHub Copilot
  • Learn how to address boundary conditions and refine GitHub Copilot-generated suggestions
  • Debug and fix broken code using GitHub Copilot insights to enhance application reliability

Project

Use GitHub Copilot to build and test a Unit Converter console app in Java, focusing on length (meters ↔ feet), temperature (Celsius ↔ Fahrenheit), and length (inches ↔ centimeters) conversions with comprehensive unit tests.

Prerequisites

  • Java Development Kit (JDK) installed
  • JUnit downloaded and libraries made available
  • VS Code or IntelliJ IDEA with GitHub Copilot enabled
  • Active GitHub Copilot subscription

Steps

Step 1: Project setup

  • Create a new Java project named JavaUTDemo that uses Maven
  • Add/Open Main.java file and replace the code with this:
public class Main {
    public static void main(String[] args) {
        // Clear terminal window (commented out as it may not work on all systems)
        System.out.print("\033[H\033[2J");
        System.out.flush();

        UnitConverter converter = new UnitConverter();
        testConvertMetersToFeet(converter, 10);
        testConvertCelsiusToFahrenheit(converter, 25);
        testConvertInchesToCentimeters(converter, 5);
    }

    private static void testConvertMetersToFeet(UnitConverter converter, double meters) {
        double feet = converter.convertMetersToFeet(meters);
        System.out.println(meters + " meters is " + feet + " feet.");
    }

    private static void testConvertCelsiusToFahrenheit(UnitConverter converter, double celsius) {
        double fahrenheit = converter.convertCelsiusToFahrenheit(celsius);
        System.out.println(celsius + " degrees Celsius is " + fahrenheit + " degrees Fahrenheit.");
    }

    private static void testConvertInchesToCentimeters(UnitConverter converter, double inches) {
        double centimeters = converter.convertInchesToCentimeters(inches);
        System.out.println(inches + " inches is " + centimeters + " centimeters.");
    }
}
  • Add UnitConverter.java file and replace the code with this:
public class UnitConverter {
    // Implement length conversion methods
    public double convertMetersToFeet(double meters) {
        return meters * 3.28084;
    }
    public double convertFeetToMeters(double feet) {
        return feet / 3.28084;
    }
    // Implement temperature conversion methods
    public double convertCelsiusToFahrenheit(double celsius) {
        return (celsius * 9 / 5) + 32;
    }
    public double convertFahrenheitToCelsius(double fahrenheit) {
        return (fahrenheit - 32) * 5 / 9;
    }
    // Implement length conversion methods
    public double convertInchesToCentimeters(double inches) {
        return inches * 2.54;
    }
    public double convertCentimetersToInches(double centimeters) {
        return centimeters / 2.54;
    }
}
  • Build and run the app

Step 2: Create Length Conversion Unit Tests

  • Highlight the convertMetersToFeet method body, right-click and select Copilot > Generate Tests or you can use Inline Chat as an alternative.
  • Paste the code into UnitConverterTest.java file
  • Configure project as needed and run unit tests

Step 3: Create Additional Unit Tests

  • Highlight the other conversion methods, right-click and select Copilot > Generate Tests or you can use Inline Chat as an alternative.
  • Paste the code into UnitConverterTest.java file
  • Run all unit tests

Step 4: Test for Boundary Conditions

  • With the UnitConverterTest.java file opened, in GitHub Copilot Chat (not Inline Chat) ask:
Create additional unit tests for all boundary conditions
  • Paste the code into UnitConverterTest.java file
  • Run all unit tests

Step 4: Test for Additional Boundary Conditions

  • With the UnitConverterTest.java file opened, in GitHub Copilot Chat ask:
Create additional unit tests for very small positive and negative values
as well as minimum and maximum max values
  • Paste the code into UnitConverterTest.java file
  • Run all unit tests

Step 5: Test for Even More Boundary Conditions

  • With the UnitConverterTest.java file opened, in GitHub Copilot Chat ask:
Create additional unit tests to cover zero values, normal range values, and negative values
  • Paste the code into UnitConverterTest.java file
  • Run all unit tests

Step 6: Fix Broken Code With GitHub Copilot

  • Return to the UnitConverter.java file and break the convertMetersToFeet method:
public double convertMetersToFeet(double meters) {
    return meters * 3.14159;
}
  • Run all unit tests
  • In GitHub Copilot Chat ask:
Why did this unit test fail?
  • Implement the fix and re-run all unit tests

Summary

In this hands-on activity, you developed a Unit Converter application in Java with the help of GitHub Copilot as an AI coding assistant. The activity focused on testing and debugging, where you created and implemented methods for length and temperature conversions, wrote comprehensive unit tests for different scenarios, and used AI-generated suggestions to refine and debug the application. This approach highlighted the importance of testing in improving software quality and showcased how AI tools can enhance the efficiency of software development.

Resources