TestGenie is a simple command-line tool that automatically generates JUnit 5 test stubs by scanning and analyzing Java source files. It uses JavaParser to understand code structure. The JavaFileParser and TestGenerator classes handle filtering nodes and generating test stubs.
Writing test cases can be overlooked during fast-paced development or code reviews. TestGenie helps by analyzing Java classes and suggesting test stubs, ensuring test coverage is considered early.
- Lowers the barrier to writing tests with autogenerated scaffolding.
- Encourages developers to add proper unit tests by providing a clear starting point.
git clone https://github.com/yourname/testgenie.git
cd testgenie
./gradlew clean build
Run the tool on Java source files in the /samples folder:
./gradlew run --args="--input samples/BankAccount.java --output output"
Output test stubs will be in the /output directory.
You can replace BankAccount.java with any Java file in /samples:
./gradlew run --args="--input samples/Calculator.java --output output"
./gradlew run --args="--input samples/StringUtils.java --output output"
View generated tests via terminal or open in your editor:
cat output/CalculatorTest.java
- Parses any .java file placed in /samples.
- Analyzes methods, arguments, conditionals, exceptions.
- Generates clean, compilable JUnit 5 tests in /output.
- Supports flags to generate specific test stubs.
- Supports ignore flags to exclude certain test stubs.
Below is a list of all test scenarios that I generate if they exist in your Java source file:
Scenario | Description |
---|---|
Null checks | if (arg == null) or Objects.requireNonNull |
Exceptions | throw new ... or assertThrows(...) |
Conditionals | if , switch |
Optional return | Optional.of , Optional.empty() |
Boolean return | return true/false or comparison |
State change | Non-final instance variables, e.g., balance += |
You can control which test stubs are generated by using --flag and --ignore options.
Specify which test stubs should be generated.
If no --flag is provided, all test stubs will be generated by default.
Multiple flags can be provided either by repeating the option or by using a comma-separated list.
Examples:
# Generate all stubs (no flags)
./gradlew run --args="--input samples/StringUtils.java --output output"
# Generate only exception-related stubs
./gradlew run --args="--input samples/StringUtils.java --output output --flag exceptions"
# Generate exception and null-check stubs
./gradlew run --args="--input samples/StringUtils.java --output output --flag exceptions,nulls"
Specify which test stubs to skip during generation.
- Works in combination with --flag or on its own.
- Multiple ignore flags can be provided in the same way as --flag.
Examples:
# Generate all stubs except exceptions
./gradlew run --args="--input samples/StringUtils.java --output output --ignore exceptions"
# Generate only null-check stubs and skip exceptions
./gradlew run --args="--input samples/StringUtils.java --output output --flag nulls --ignore exceptions"
Flag | Description |
---|---|
exceptions |
Generate stubs for methods that throw exceptions. |
nulls |
Generate stubs for null-check scenarios. |
logging |
Generate stubs to verify logging behavior. |
performance |
Generate stubs for performance or timing-related tests. |
boundary |
Generate stubs for boundary and edge case input scenarios. |
- Generation logic: src/main/java/com/testgenie/TestGenerator.java
- Parsing logic: src/main/java/com/testgenie/JavaFileParser.java
- CLI commands: src/main/java/com/testgenie/App.java
- Java 17 or later (tested with Java 22)
- Gradle 7.0 or later (use the included ./gradlew wrapper)
Make sure your JAVA_HOME is set and ./gradlew is executable.
Add your own Java files to /samples and run TestGenie on them. Generated test files will be named after the original class with a Test suffix (e.g., Calculator.java → CalculatorTest.java).