Generating unit tests
watsonx Code Assistant
Generate unit tests for various programming languages from the chat, the CodeLens in the editor, or the context menu.
The following table lists the type of unit test for each plan.
Plan | Unit test | Description |
---|---|---|
Trial plan Essentials plan | Basic | Uses generative AI to provide a basic unit test. No extra code analysis is required. |
Standard plan | Enhanced | Requires a built application, and uses a code analysis and generative AI to provide an enhanced unit test for Java methods and classes. |
Eclipse IDE only: For best results with a multimodule Java application, ensure that you import the aggregator module and each submodule as separate projects within the Eclipse workspace. Watsonx Code Assistant is unlikely to successfully introspect your project if you import the top-level directory as a single project.
Language support
Test generation is available for the following languages:
- C
- C++
- Go
- Java
- JavaScript
- Python
- TypeScript
Dependencies
Standard plan For Java, make sure that the pom.xml
file includes the following dependencies:
- JUnit - A minimum of JUnit 5 is required
- Mockito - A minimum of Java 9 is required
The following sample pom.xml
file shows the dependencies with example versions. In your file, include the dependencies and versions that you are using.
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Using a chat command to generate a unit test
You can use the /unit-test
command in chat to generate a test for a referenced class, file, function, or method in the active workspace.
Use this syntax:
/unit-test [using <test_framework>] <code reference> [similar to (<unit test reference>)] [additional instructions]
-
[using <test_framework>]
is optional. If you don't specify the framework, watsonx Code Assistant uses the test framework from your settings. -
For
<code reference>
, type the@
symbol to see a list of classes, files, functions, and methods from your workspace. Use one class, file, function, or method reference at a time. -
[similar to (<unit test reference>)]
is optional. Use to provide an example of how the generated unit test is expected to look. You can use a method reference in thesimilar to
option if required. -
The
[additional instructions]
are optional. Add instructions if you want specific details.
Next, watsonx Code Assistant processes the request and in the chat displays the generated unit test for the code that you selected.
-
Essentials plan Copy the unit test to a file where you have unit tests for your application.
-
Standard plan Copy the unit test to a file in the
/test
folder. For example, copy the unit test file to.../src/main/test/java/com/acme/modres/OrdersAlertFilter.java
.
Using the CodeLens in the editor to generate unit tests
In the IDE editor, the CodeLens shows a line of generative AI options that precedes code blocks and snippets.
Known issue If you are using the watsonx Code Assistant for Enterprise Java Applications extension, upon startup it might take a few seconds for the enhanced Java capabilities to be available from the CodeLens.
-
Click the Unit Test option that immediately precedes a code block to generate a unit test.
In the following code example, the
Explain | Document | Unit Test
options immediately precede theprotected void
keywords.CodeLens example The watsonx Code Assistant chat window opens, displays the
/unit-test @<*item name*>
command, runs the command, and displays the unit test. -
Essentials plan Copy the unit test to a file where you have unit tests for your application.
-
Standard plan Copy the unit test to a file in the
/test
folder. For example, copy the unit test file to.../src/main/test/java/com/acme/modres/OrdersAlertFilter.java
.
Generating unit tests from the context menu
To generate a unit test from a context menu:
-
In the Explorer view, expand your application to the code that you want to generate a unit test for.
-
Right-click the code, click watsonx Code Assistant, then click Unit Test.
-
The watsonx Code Assistant chat displays the
/unit-test
command for the code that you selected for unit testing. The following syntax is used in the command:/unit-test @<*item syntax*>
The following syntax examples are for a file, a class, and a method.
- File:
/unit-test @OrdersAlertFilter.java
- Class:
/unit-test @OrdersAlertFilter
- Method:
/unit-test @DoFilter()
Watsonx Code Assistant processes the request and in the chat displays the generated unit test for the code that you selected.
- File:
-
Essentials plan Copy the unit test to a file where you have unit tests for your application.
-
Standard plan Copy the unit test to a file in the
/test
folder. For example, copy the unit test file to.../src/main/test/java/com/acme/modres/OrdersAlertFilter.java
.