labs / tiddlers / content / labs / lab03 / _Labs_03_Testing a Data Access

Create tests for the StudentCollectionsDAO as shown in last week's lab. This is a fairly typical DAO class that is using collections to store the data that is similar to the DAO class that you created in INFO 201.

  1. Generate the test classes as shown in section <> of last week's lab.

    You will be using the StudentCollectionsDAO class rather than the Calculator class that is described in that section.

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

  3. Make all five tests fail by adding the following to each test method:

    fail("not implemented yet");
  4. Show the test results pane using <

    IDE Tools > Test Results">>.

  5. Run the tests using <

    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 <>.

    You should see that all five tests fail.

  6. Implement and test each test, one by one. Remove the fail call when you think you have finished a test.

    The Invoice test from section <> from last week's lab is similar to what is needed here --- swap Invoice for StudentCollectionsDAO and InvoiceItem for Student 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 students from the DAO (refer to section <> which covered this topic.

    Create three student objects in the setUp, but only save two of them in the DAO object. This gives you some data to test most of the methods with. The third student that has not been saved can be used to test the save method.

    All of your tests should pass when you are finished.

  7. Test your tests. Intentionally break the DAO methods, one by one --- for the save and delete methods you can comment out the line that adds/removes the student. For the get methods you can method you can return either an empty ArrayList or an empty Student.

    Your tests should fail --- if not then your tests are not sufficient.

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

  9. Commit and push.