We need to add some bits and pieces to the build.gradle
file to add support for unit testing to the project. You can find the file in NetBeans under the <> folder in the projects pane.
Add the jacoco
plugin to the plugins
section. The complete section should look like:
plugins { id "application"; id "jacoco"; }
We will explain what JaCoCo is in a later section.
Add the testing library dependencies to the project. The following section should be added to the bottom of the build script at the root level (not inside any other curly braces):
dependencies { def junitVer = "5.8.2"; testImplementation group: "org.junit.jupiter", name: "junit-jupiter-api", version: junitVer; testRuntimeOnly group: "org.junit.jupiter", name: "junit-jupiter-engine", version: junitVer; testImplementation group: "org.hamcrest", name: "hamcrest", version: "2.2"; }
There are three frameworks being used here:
We will explain what each of these frameworks is doing as we go.
Add the test
section to the bottom of the build script at the root level to tell Gradle which testing framework to use when running automated tests:
test { useJUnitPlatform(); }
Save the file.
You may get some warnings in your project at this point. This is because your project is using libraries that Gradle has not yet downloaded. Build the project using <
Build Project">> --- this will force Gradle to download everything it needs and should fix any problems.