Table of Contents

Tutorial: Test Java App

This tutorial shows you step by step how to create a model and write the java handler to test your java app.

We will use the example of a simple coin operated vending machine application.

Test Requirements

Below is the requirements/specifications on how the vending machine should work:

it takes a quarter or a half-dolloar coin only, one coin at a time

it automatically vends when the coins accumulate to $1.00 (or more)

when it vends, it returns the change immediately

customer can push “Cancel” button to cancel the transaction at any time.

Our task is to test VendingMachine.java, a simulation of the vending machine. There are only 4 methods in this java class: addQuarter(), addHalfDollar(), cancel() and getSum().

Modeling Java AUT

First we need to create an abstract model with the behaviour described above. The state of the system is identified by the total value of all coins inserted. So we have 5 states: “0”, “25c”, “50c”, “75c”, “100c” with “0” being the initial state.

You may notice that there is another state “125c”. That is true but since the vending machine automatically vends and returns the change at the same time, we decided to model this situation by introducing a “Vending” state to indicate minimum of “100c” has reached. This state can be reached from two states: “50c” with a half-dollar coin and “75c” with either a quarter coin or a half-dollar coin.

The resulting MBT model is:

Execute Model

Test run the model by clicking on AUT in the SideBar so that it does not have green backgroud. Then run the model as usual. Since AUT is not activate, the system will just generate test cases for you.

where nn is number of cents for the state, replaces it with the correct number of cents expected.

The java handler is completed. To deploy, compile the java handler and copy .class file to /bin folder of TestOptimal folder. If you are running TestOptimal inside Eclipse or NetBeans, IDE does this for you automatically.

Execute Model

Now we can run the model with the TestOptimal browser. You can also execute this model from JUnit. Click HERE for the tutorial that shows how to execute TestOptimal from JUnit.