4 Test class

Rudolf Pecinovsky

Contents of whole series

Note:
You can download the project we will use in this lesson here.

Last time you said that everything, that I want to remember, I can save on the disk. Can I even save my experiments?

You can – e.g. in such a way that you define a class and teach it to react to some message so that it repeats your remembered action once more. BlueJ can watch your activities and create a program that will repeat it on demand. It works in a similar way to macro recorders in some office programs.

I would like to try it.

Why not, come and try. However I should point out that BlueJ offers this possibility only for test classes. Other types of classes you have to program by a standard way – it means by writing a program in a text form. Nevertheless until we start to write programs in such a way, this BlueJ ability will be very useful to us.

Test fixture

So show me how I can create such a class.

First, I have to say something about test classes in general for you to understand why we make some steps in the demonstrated way.

As you have surely guessed from the term “test class”, we use these types of classes for testing of programs. However we usually need several tests for testing a part of a program. Therefore it is very often useful to have one common starting point for a whole group of tests. You can take a test class as an object that is able to perform a group of tests with given common starting point.

The program that prepares the starting point, which is common for all tests in the group, we call test fixture. When we ask the test class to run any prepared test, it first prepares the test fixture and then runs the test to test this fixture.

First of all we have to show the test class, how to prepare the test fixture, and then we can start to make some experiments with it and pass off these experiments as tests.

Creation of a new class

So if I understand well we should start with creating a test fixture.

Almost. Before we start to create the fixture, we have to create the test class. Therefore we start by creating a test class.

Don't play words with me – it's after all the plain fact. So let's continue!

As you can guess, you ask for a new class by pressing the button New class on the button pane. BlueJ then opens a dialog box where you should enter the name of the created class together with the type of class you are asking for.

If you allow I will put the explanation of rules for creating names aside and explain it in the future. For now I suggest to name the class Test – enter this name into the text field Class Name. Then set the switch Class Type to Unit Test, because we said that BlueJ is willing to program only the test class for us. Then confirm your settings.

Figure 1: Creating a new test class named Test

BlueJ creates a new class, names it Test and shows it in a free area of the class diagram.

Take a notice of its color. As we said, the test class offers special features and therefore BlueJ identifies the class by its color.

Creation of a test fixture

I have the class. So next to create a fixture! What should I do?

Nothing you don't already know. You will start with the reset of the virtual machine and then you will simply send messages that bring the system into a state where you want the tests to start. BlueJ will monitor your actions and when you ask it, it converts these actions into a program that prepares the fixture and puts the system in the requested state.

Wait, wait. Surely I have to tell it somehow that I am starting to show it how to prepare the fixture.

BlueJ remembers all the actions from the last compilation or virtual machine reset. If you start your “exhibition” just after compilation, you needn't tell it anything. If you make some experiments before the “exhibition”, you should start by resetting the virtual machine.

I understand. And how do I tell BlueJ that I have finished?

You send BlueJ the message Object Bench to Test Fixture, which you find in the context menu of the test class. BlueJ then creates the relevant program. You can check its function by sending the message Test Fixture to Object Bench, which makes BlueJ run the prepared program and fill the object bench with particular references (with closed eyes we can say: with particular objects) – with the test fixture.

Figure 2: Command asking BlueJ to convert the remembered actions into a program for creating the test fixture

I made it. I created a rectangle, an ellipse and a triangle repeating our actions from the last lesson. When I asked for creating fixture then some arrows popped up in the class diagram near the test class.

That's right. By creating the fixture the shape classes were used and therefore BlueJ drew dependency arrows from the test class to the used shape classes. Move the test class a little to the right and you will see the arrows going from the class Test to classes Rectangle, Ellipse and Triangle.

Creation of a test

You are right. So the fixture is ready. How should I now prepare a test?

I again recommend starting with reset of the virtual machine to be sure that both you and BlueJ have the same opinion about the test program configuration at the start of test. Then you again open the class context menu and enter command Create Test Method. So you send a message to BlueJ telling that you want to start to show BlueJ a test of the fixture of the given class.

BlueJ lets the test class create the test fixture and it prepares buttons for finishing and canceling the demonstrated test in the button pane together with an indicator of recording. Simultaneously it opens a dialog box asking how you want to name the created test.

Let's agree that you name it movements. As you enter and confirm the name, BlueJ again starts to monitor your actions. It marks this monitoring by a “red light” in the button pane.

If you realize in the middle of demonstration that you have done something wrong, press the button Cancel and you can start again from the beginning.

If everything is right, after finishing the demonstration press the button End. BlueJ switches off the red light, converts the remembered actions into program and includes this program into the test class. Then it adds the command for running the just created program into the context menu of the test class.

Everything we have talked about I have tried to demonstrate in an accompanying animation. Run it for revision and try to do everything once more.

Click here to run annimation

Animation 1: Creating of a test class and its fixture

Everything worked. However I wonder why after every movement an area without color remains in the original position of the moved object although there is another object there.

The objects move in the same way as they were painted on the paper. They should be first rubbed out at the original position and then painted in at the new one. Because the objects don't know about one another they cannot send a message to the underlying objects to repaint themselves. It's your task to explicitly ask the damaged object to repaint.

I deliberately made it in this simple and we can say also clumsy way to motivate you when we switch to a more clever canvas. You will have to learn some new constructions and relations for this transformation.

I know a whole set of simple controlled programs, which don't behave so stupidly and I need no special knowledge to control them.

Naturally I could modify the program to behave more cleverly. However our goal isn't moving objects but learning programming. If I modify the program to behave cleverly, I should hide some constructions and techniques that allow this behavior, because you cannot understand them yet. When we need to introduce these techniques, you would wonder, what they were for when you have not needed them till that time.

Stand still for some time it won't take a long.

I still have one question. All the time you say that we send messages to objects. However the command in the context menu is “Create Test Method”. What is it the method?

Method is a part of a program, which takes care of the object reaction to the received message. Each of the messages the object understands, has its own method that performs the desired action. Hence every sending of a message is transformed into calling the corresponding method.

When we write programs, we will define classes and their methods. Because we wanted BlueJ to create a program that does the just showed test, we wanted it to define a method that ensures the performing of this action.

Programmers mostly don't use the term sending messages; they prefer the term calling method. Both terms are synonyms. When we speak about a problem in a more abstract level, we largely prefer the term sending messages, when we speak about it in the level of code, we mostly prefer the term calling methods.

The program that creates a fixture is also defined as a method. When we send a message asking for running of our test, BlueJ in cooperation with a testing class ensures calling the method, which creates the test fixture, followed by calling the method, which performs our test.

Revision

Revise once more what we have learned today:

  • BlueJ is able to monitor our performance, convert it into program and save this program into a test class.
  • The test classes are constructed as sets of several tests applied to the same starting set of objects. We name this starting set of objects the test fixture. Every test class has test fixture of its own.
  • We can create the test fixture in such a way that we let BlueJ compile classes or reset the virtual machine and then we demonstrate how the test fixture should be prepared. Using command Object Bench to Test Fixture in the context menu of the class, for which we want prepare the fixture, BlueJ converts the performed actions into program that will be run before every test of this class.
  • The program creating the test fixture can be run independently using command Object Bench to Test Fixture. BlueJ then fills the object bench with the references to objects in the test fixture.
  • We ask for creation of a new test using command Create Test Method in the context menu of a test class. BlueJ first asks for a name of the created test and then it shows on the button pane the buttons for finishing and canceling the recording of performed actions together with an indicator of recording.
  • We finish the demonstration using the button End in the button pane. BlueJ then converts the demonstrated steps into a program, adds the program into the test class and adds the corresponding command into the context menu of this class.

Note:
You can download the current version of the project here.

Contents of whole series

Rudolf Pecinovsky
(rudolf.pecinovsky@i.cz) is a senior EDU expert in ICZ, Inc. and associate professor of software engineering at The University of Economics, Prague. He has more than 20 years experience in programming education. Rudolf published 39 books in five national languages. His latest book on design patterns was launched in September 2007.

Project Features

Project Links

About this Project

edu was started in November 2009, is owned by Antonin Nebuzelsky, and has 87 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close