Skip to main content

Tutorial: Generate LLM test suite for function


For this example, we used Claude 3.5 Sonnet to generate tests. We configured Symflower's extension settings as follows:

  • Symflower > LLM:Model: anthropic/claude-3.5-sonnet
  • Symflower > LLM > Provider:Token: your API token

We started with the following source code:

package com.symflower.area;

public class Circle {
public static double CircleArea(double radius) {
if (radius <= 0) {
throw new IllegalArgumentException("radius must be positive");
}

return Math.PI * radius * radius;
}
}

For this example, we then ran Symflower: Generate Test Suites for Function via LLM in Visual Studio Code. This resulted in the following output:

package com.symflower.area;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.*;

class CircleTest {

@Test
void testCircleAreaPositiveRadius() {
double radius = 5.0;
double expectedArea = Math.PI * radius * radius;
double actualArea = Circle.CircleArea(radius);
assertEquals(expectedArea, actualArea, 0.0001, "Area calculation for positive radius should be correct");
}

@Test
void testCircleAreaZeroRadius() {
double radius = 0.0;
Executable executable = () -> Circle.CircleArea(radius);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, executable, "Should throw IllegalArgumentException for zero radius");
assertEquals("radius must be positive", exception.getMessage(), "Exception message should match");
}

@Test
void testCircleAreaNegativeRadius() {
double radius = -5.0;
Executable executable = () -> Circle.CircleArea(radius);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, executable, "Should throw IllegalArgumentException for negative radius");
assertEquals("radius must be positive", exception.getMessage(), "Exception message should match");
}
}