labs / tiddlers / content / labs / lab03 / _Labs_03_Testing the Product Data Access
[Lab 9]( in INFO201 was a fairly comprehensive overview of automated testing.  Rather than go over all of that again, we will point you to the relevant bits of that lab.

Those of you who have not taken INFO201 (and those of you who have forgotten how to do automated testing) should at least have a quick read through that lab --- automated testing is something that you should know about.

1.  Add an issue to GitBucket that describes that you need to create automated tests for the `ProductCollectionsDAO` class.

1.  We need to modify the build script to include the libraries for testing.  It is time that we fixed up the outdated Gradle code that NetBeans generated when the project was created, so replace the entire contents of your `build.gradle` file with the following (delete everything in that file and replace it with the code below):

    plugins {
        id 'application'
        id 'jacoco'

    repositories {

    dependencies {
        def junitVer = '5.7.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'

    test {

    run {
        standardInput =

    task openProjectFolder {
        group = "Directories"
        description = "Open the project root in the system file manager."
        doFirst {
            println('Opening: ' + file(projectDir))

    def ignored="""

    task createGitIgnore {
        group = "Git"
        description = "Create the project's .gitignore file."
        doLast {
            def file = new File(projectDir, ".gitignore")
            if ( !file.exists() ) {
                println('Creating .gitignore')
                file.text = ignored
            } else {
                println('.gitignore already exists')

    mainClassName = 'Main'

1.  Save the `build.gradle` file and rebuild the project using <<menu "RCTP > Build">>.  You should see Gradle downloading the necessary libraries  by clicking the <<menu "Loading info202_project">> notification at the bottom right of the NetBeans status bar.  You will need to wait for that process to complete --- there aren't many libraries so it should be fairly quick.

1. Generate the test classes as shown in section [2.3]( of INFO201, lab 9.

    You will be using your DAO class rather than the `Calculator` class that is described in that section.

1.  Delete the generated constructor so that you won't be tempted to use it --- all initialisation should happen in the `setUp` method.

1.  Make all three tests fail by adding `fail();` to each test method.

1. Show the test results pane using <<menu "Window > IDE Tools > Test Results">>.

1.  Run the tests using <<menu "Run > Test Project">>.  Gradle will search the project for test methods and run them all.  You can also test a single file as per usual by right clicking it in either the source editor or project pane and selecting <<menu "Test File">>.

    You should see that all three tests fail.

1.  Implement and test each test, one by one.  Remove the `fail` call when you think you have finished a test. Section [3]( of the INFO201 lab has a fairly similar example --- swap `Invoice` for `ProductCollectionsDAO` and `InvoiceItem` for `Product` and you are most of the way there.

     Since the DAO collection is static you will need to use the `tearDown` method to remove all of the test products from the DAO (refer to section []( of INFO201 lab 9 for an explanation).

    Create three products in the `setUp`, but only save two of them in the DAO object.  This gives you some data to test the `removeProduct` and `getProducts` methods with.  The third product that has not been saved can be used to test your `saveProduct` method.

     The code in the test methods from INFO201 lab 9 has everything that you need to test your DAO methods.

    All of your tests should pass when you are finished.

1.  Test your tests.  Intentionally break your DAO methods --- for the `saveProduct` and `removeProduct` methods you can comment out the line that adds/removes the product.  For the `getProducts` method you can return an empty `ArrayList`.

     Your tests should fail --- if not then your tests are not sufficient.  The `removeProduct` test might trick you here.  If you don't first check that the product you are about to delete exists in the DAO, then you can't actually be sure that the `removeProduct` method removed anything.

1.  Undo the changes you made to break the tests, and run the tests again.  They should pass again.

1.  Commit and close the issue.