Java CLI - olganaumenko/testwikisync GitHub Wiki
UnitTestBot Java CLI allows you to automatically generate human-readable ready-to-use unit tests for Java code. With CLI, you can streamline manual unit testing or integrate UnitTestBot Java into your CI/CD process.
UnitTestBot Java CLI is distributed as a JAR file. You can find the implementation in the utbot-cli module.
Check the tutorial to get started with UnitTestBot Java CLI (examples are provided for Windows):
- System requirements
- How to download
- How to generate tests for a single class
- How to generate tests for multiple classes
- How to run tests
- Commands, options, arguments
- JDK 17: installed.
- JAVA_HOME: contains the path to JDK 17 installation directory, e.g.,
C:\Program Files\Java\jdk-17
. - PATH: contains the path to the
bin
folder of the JDK 17 installation directory, e.g.,C:\Program Files\Java\jdk-17\bin
.
- Go to UnitTestBot Java builds.
- Find the latest βgreenβ build for
main
, e.g., try this valid one. - Scroll down to Artifacts and download the zip-archive named like
utbot-cli-java-main-VERSION
. - Create a directory (e.g.,
utbot
) and unzip the archive into it.
You may keep your source and test files as well as the utbot-cli-java-main-VERSION.jar
file anywhere you like, but
you should specify the proper paths as options. To keep it simple, we locate everything in one place.
- In your new directory with the
utbot-cli-java-main-VERSION.jar
inside, create ansrc
folder for your source files under test, abuild
folder for the complied.class
files, and atest
folder for the resulting tests, e.g.:
βββ utbot
βββ build
βββ src
βββ BitOperators.java
βββ test
βββ utbot-cli-java-main-VERSION.jar
- Compile the source
.java
file and put the resulting.class
file into thebuild
folder, e.g.:
javac src\BitOperators.java -d build
Now you have this:
βββ utbot
βββ build (path to compiled source or test classes)
βββ org
βββ utbot
βββ examples
βββ math
βββ BitOperators.class
βββ src
βββ BitOperators.java (source file)
βββ test (path for test file)
βββ utbot-cli-java-main-VERSION.jar
- Generate tests for the chosen source file using the
generate
command (refer to the upper tree to understand the relevant paths):
java -jar utbot-cli-main-VERSION.jar generate --source <path to source file> --classpath <path to compiled source class> --output <test file with path> <TARGETCLASSFQN: fully qualified name for source class>
Example:
java -jar utbot-cli-main-2023.6.4550.jar generate -s src\BitOperators.java -cp build -o test\BitOperatorsTest.java org.utbot.examples.math.BitOperators
Make sure you use the --source, -s
and --classpath, -cp
required options and pass the TARGETCLASSFQN
argument,
which is the target class fully qualified name.
We recommend using the--output, -o
option for consistency.
- Keep the source files in the
src
folder. - Compile the source
.java
files and put the resulting.class
files into thebuild
folder. - Generate tests for the chosen source files using the
bunchGenerate
command:
java -jar utbot-cli-main-VERSION.jar bunchGenerate --classpath <path to compiled source classes> --output <path to test files> --filter <package to test> <CLASSROOTDIRECTORY: path to compiled source classes>
Example:
java -jar utbot-cli-main-2023.6.4550.jar bunchGenerate -cp build -o test -pf org.utbot.examples.math build
Make sure you use the --classpath, -cp
required option and pass the CLASSROOTDIRECTORY
argument, which is the
folder containing the compiled source classes.
We recommend using the--output, -o
option for consistency.
With the --filter, -pf
option, you can specify the source files to generate tests for. If the fully qualified name of
the given source class contains the specified string, the tests are generated for this class.
- Compile the generated test and put the resulting
.class
file into thebuild
folder, e.g.:
javac -cp build;utbot-cli-main-2023.6.4550.jar -d build test\BitOperatorsTest.java
Make sure to include the following paths to the classpath:
- the path to the folder where you put the compiled
.class
files, e.g.,build
; - the path to a test framework library (JUnit 4, JUnit 5, TestNG) or the
utbot-cli-java-main-VERSION.jar
file.
Now you have this:
βββ utbot
βββ build
βββ org
βββ utbot
βββ examples
βββ math
βββ BitOperators.class
βββ BitOperatorsTest.class
βββ src
βββ BitOperators.java
βββ test
βββ BitOperatorsTest.java
βββ utbot-cli-java-main-VERSION.jar
- Run the compiled test class:
java -jar utbot-cli-main-VERSION.jar run --classpath <path to compiled test class> <fully qualified name for test class>
Example:
java -jar utbot-cli-main-2023.6.4550.jar run -cp build org.utbot.examples.math.BitOperatorsTest
Basic usage for UnitTestBot Java CLI requires you to specify commands, options, and arguments:
java -jar utbot-cli-main-VERSION.jar [GENERAL OPTIONS] COMMAND [COMMAND OPTIONS] ARGS
Option | Value | Effect |
---|---|---|
--verbosity | [ERROR | WARN | INFO | DEBUG | TRACE] | Change verbosity level (case-insensitive). |
--version | Show UnitTestBot Java CLI version and exit. | |
-h, --help | Show help message and exit. |
Generate tests for the specified class:
java -jar utbot-cli-main-VERSION.jar [GENERAL OPTIONS] generate [COMMAND OPTIONS] TARGETCLASSFQN
TARGETCLASSFQN
β target class fully qualified name.
Option | Value | Effect |
---|---|---|
-m, --mock-strategy | [do-not-mock | package-based | all-except-cut] | Define the mocking strategy |
--test-framework | [junit4 | junit5 | testng] | Test framework to be used |
--mock-statics | [do-not-mock-statics | mock-statics] | Choose framework for mocking statics (or not mock statics at all |
-f, --force-static-mocking | [force | do-not-force] | Forces mocking static methods and constructors for "--mock-always" classes |
--overflow | [ignore | error] | Treat overflows as errors |
--mock-always, --ma | TEXT | Classes fully qualified name to force mocking theirs static methods and constructors (you can use it multiple times to provide few classes)default classes =java.util.Random org.slf4j.Loggerorg.slf4j.LoggerFactoryorg.utbot.api.mock.UtMock |
--generation-timeout | INT | Specifies the maximum time in milliseconds used to generate tests (1200000 by default) |
-o, --output | TEXT | Specifies output file for a generated test |
-cp, --classpath | TEXT | Specifies the classpath for a class under test |
-s, --source | TEXT | Specifies source code file for a generated test |
--project-root | TEXT | Specifies the root of the relative paths in the sarif report that are required to show links correctly |
--sarif | TEXT | Specifies output file for the static analysis report |
-l, --language | [Java | Kotlin] | Defines the codegen language |
-p, --print-test | Specifies whether a test should be printed out to StdOut | |
-h, --help | Show this message and exit |
Generate tests for class files in the specified directory.
java -jar utbot-cli-main-VERSION.jar [GENERAL OPTIONS] bunchGenerate [COMMAND OPTIONS] CLASSROOTDIRECTORY
CLASSROOTDIRECTORY
β directory with classes.
Option | Value | Effect |
---|---|---|
-m, --mock-strategy | [do-not-mock | package-based | all-except-cut] | Defines the mock strategy |
--test-framework | [junit4 | junit5 | testng] | Test framework to be used |
--mock-statics | [do-not-mock-statics | mock-statics] | Choose framework for mocking statics (or not mock statics at all |
f, --force-static-mocking | [force | do-not-force] | Forces mocking static methods and constructors for "--mock-always" classes |
--overflow | [ignore | error] | Treat overflows as errors |
--mock-always, --ma | TEXT | Classes fully qualified name to force mocking theirs static methods and constructors(you can use it multiple times to provide few classes);default classes = java.util.Random org.slf4j.Loggerorg.slf4j.LoggerFactoryorg.utbot.api.mock.UtMock |
--generation-timeout | INT | Specifies the maximum time in milliseconds used to generate tests (1200000 by default) |
-cp, --classpath | TEXT | Specifies the classpath for a class under test |
-o, --output | TEXT | Specifies output directory. It will be populated with package-directory structure. |
-pf, --filter | TEXT | Specifies a string to generate tests only for classes that contains the substring in the fully qualified name |
--classpathFile | TEXT | Specifies the classpath as a list of strings in a file |
-l, --language | [Java | Kotlin] | Defines the codegen language |
-h, --help | Show this message and exit |
Runs tests for the specified class.
java -jar utbot-cli-main-VERSION.jar run [GENERAL OPTIONS] run [COMMAND OPTIONS] CLASSWITHTESTSNAME
CLASSWITHTESTSNAME
β specifies a class with tests.
Option | Value | Effect |
---|---|---|
-cp, --classpath | TEXT | Specifies the classpath for a class with tests |
--test-framework | [junit4 | junit5 | testng] | Test framework to be used |
-o, --output | TEXT | Specifies output file with specified extension for tests run information |
-h, --help | Show this message and exit |