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.
Add an issue to GitBucket that describes that you need to create automated tests for the ProductCollectionsDAO
class.
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):
```gradle plugins {
id 'application' id 'jacoco'
}
repositories {
mavenCentral()
}
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 {
useJUnitPlatform()
}
run {
standardInput = System.in
}
task openProjectFolder {
group = "Directories" description = "Open the project root in the system file manager." doFirst { println('Opening: ' + file(projectDir)) java.awt.Desktop.getDesktop().open(file(projectDir)); }
}
def ignored=""" .gradle build .zip .tgz """
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' ````
Save the build.gradle
file and rebuild the project using <
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.
Delete the generated constructor so that you won't be tempted to use it --- all initialisation should happen in the setUp
method.
Make all three tests fail by adding fail();
to each test method.
Show the test results pane using <
IDE Tools > Test Results">>.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 three tests fail.
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 3.3.2.1 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.
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 get 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.
Undo the changes you made to break the tests, and run the tests again. They should pass again.
Commit and close the issue.