Writing unit tests within the IntelliJ IDE
A quick guide to writing unit tests for the demonstration project, java-demo
, using the Diffblue Cover IntelliJ plugin.
Importingjava-demo
The java-demo
application is a simple result-checker for the traditional game of "Tic Tac Toe" (or "Noughts and Crosses") which can be found at https://github.com/diffblue/java-demo.
Start IntelliJ without a project loaded to see a welcome screen:
Choose Git
from the Check out from Version Control
option. Enter the java-demo
url into the repository box, selecting an appropriate location for the project.
Once it has been successfully cloned, expand the java-demo
section under Project
to see the source tree.
Writing new tests
java-demo
contains one existing test class, OrderTest.java
.
In order to generate new tests for a class which is not currently covered, select the TicTacToe
class in the Project
tool window. Right-click * and select the Write Tests
option from the menu.
The plugin builds the project before analyzing the code and creating the tests. Once the project has been built, click the progress bar at the bottom of the screen to open a pop-up window.
The progress window will show the different stages that the analysis is going through. While the code is being analyzed, it is possible to view current progress, but with java-demo
that will be a very short time-frame.
Once the new tests have been created, they will be automatically integrated into the project under src/test/java
.
* Note that it is also possible to Write Tests
by right-clicking the following:
- individual classes from within the source editor
- individual methods from within the source editor
- individual classes from the tab at the top of the source file
- individual methods from within their classes in the
Project
menu (ifShow Members
has been selected after right-clicking on theProject
menu)
Elsewhere within the IDE, for example in test classes or non-Java files, the Write Tests
feature is disabled.
Running the tests
After Diffblue Cover has written tests for your project, they will be automatically integrated into your test suite.
Tests created by Diffblue Cover make use of the JUnit testing framework so junit
is included in the java-demo
project dependencies in the pom.xml
.
Select the new TicTacToeTest
class in the project explorer, right-click and Run 'TicTacToeTest'
.
This will then show the results of the JUnit run: