Tutorial 4 - Testing Your Concrete Class
Tutorial 4 - Testing Your Concrete Class
Tutorial 4 – Testing your concrete class
Purpose:
The purpose of this tutorial is to show how you can make use of a separate class, an
application class, to test the functionality of a concrete class that you are creating. On
many occasions you will receive a question with a UML class diagram for which you are to
create the concrete class for. It does not mean that your class is totally correct if you
created the class, and it compiles without any syntax errors. You still need to test your
logic, especially in the helper methods. You may want to do this during an assessment, it
does not take a lot of time. Let’s get started.
We will use the following question as an example to help explain how you must test the
class that you are creating.
Start by creating a new NetBeans project called RoachProject. When creating this project,
make sure you also create a main class as highlighted in the figure below.
Page 1
Add a new Java class to the project called RoachPopulation and complete the class as
shown in the UML class diagram and description below.
RoachPopulation
‐ population: int
+ RoachPopulation()
+ setPopulation(int): void
+ getPopulation(): int
+ doubling(): void
+ spray(): void
RoachPopulation UML Class Diagram
The default constructor will assign 3 as the initial population of roaches.
You have you normal mutator and accessor methods.
The doubling() method will double the population of the roaches.
The spray() method will decrease the roach population by 100% if there is 20 or less
roaches and decrease the population by 50% if there is more than 20 roaches.
1. Add the data member as shown in figure 1.
Figure 1
2. Make use of the Insert Code feature and insert the constructor and the get and set
methods as shown in figure 2.
Figure 2
Page 2
3. You need to ask yourself the following question now:
Is this code I created correct, meaning doing what it is supposed to be doing, even if
it contains no syntax errors?
There is only one way you will know, and that it to test the code by creating an object
of the class. In the main() method of our application class we will create an object/s of
this concrete class and invoke/call the methods to see if they are working correctly.
Switch to your RoachProject application class and in the main() method complete the
following steps.
4. Let’s test if the constructor, mutator and accessor methods are working of the
RoachPopulation class.
First, we will create an object of the RoachPopulation class.
RoachPopulation myHouse = new RoachPopulation();
According to the instructions of the question, there should be 3 initial roaches in the
population. So we can test if this was working correctly by invoking the
getPopulation() method to see the value of the population data member.
int thePopulation = myHouse.getPopulation();
String message = “The roach population is “ + thePopulation;
JOptionPane.showMessageDialog(null, message);
Run the RoachPopulation project with these instructions, it will display 3, which is
correct. You now know that the constructor and the accessor method is working
correct. Have a look at the code and output as shown in figure 3.
Figure 3
5. If we want to change the roach population, then we need to use the mutator method
to change the value of the population data member.
Page 3
myHouse.setPopulation(12);
thePopulation = myHouse.getPopulation();
message = “The roach population is “ + thePopulation;
JOptionPane.showMessageDialog(null, message);
We will change the population to 12 and then display the population value as shown
in the code and output in figure 5.
Figure 5
6. Always try to test only one method at a time. Once you created the method in the
concrete class, go to the application class and invoke the method to see if it functions
correctly, as we have done with the constructor, mutator, and accessor methods.
Let us complete the doubling() method now. It is stating that the roach population
will double with this method. So, complete the method as shown in figure 6.
Figure 6
7. We will now test if this method works correctly, the logic is correct, by invoking the
method in the RoachProject application class and see of the population doubled. See
the code and the output in figure 7.
a. Line 12 we created the object of the RoachPopulation class. Original population
is now 3.
b. Then at line 14 we change the population value to 12.
c. At line 16 we invoke the doubling() method to double the population, so the
population should be 24 now.
d. Rest of the code will display the population as shown in the output.
With the output correct, we know that the doubling() method is working correct.
Page 4
Figure 7
8. The spray() method is the most complicated method of this class. Follow the earlier
instruction and complete the spray() method to reduce the population of the roaches.
The code should look like that shown in figure 8. Your code could be different, if the
population get to be halved when it is more than 20 roaches.
Figure 8
9. We need to test both conditions of the spray() method. So in figure 9 you will see the
code in the RoachProject to test if the population was set to 20 or less and what the
population will be in the output after the spray() method was invoke.
a. At line 12 we created the RoachPopulation object with an initial 3 roaches.
b. At line 14 we set the population to 16 roaches.
c. At line 16 we invoke the spray() method. And because the population is less
than 20, the population should be set to 0.
d. We will display the population value in the remaining code as shown in the
output.
Page 5
If you get the wrong output, you need to go back to the method, make the needed
changes and test again. This case the output is correct.
Figure 9
10.See figure 10 for the code in the RoachProject class to test if the population was set to
more than 20 and what the population will be in the output after the spray() method
was invoke.
a. At line 12 we created the RoachPopulation object with an initial 3 roaches.
b. At line 14 we set the population to 25 roaches.
c. At line 16 we invoke the spray() method. And because the population is more
than 20, the population should be reduced by half.
d. We will display the population value in the remaining code as shown in the
output.
If you get the wrong output, you must again fix the code in the RoachPopulation class
and test again.
Page 6
Figure 10
Well done. Always try to test the methods of the concrete classes that you create by
following these steps.
Page 7