Demo Model: Vending Machine


To demonstrate how to test the java applications (java classes) with a model.


This is a modified version of Drink Vending Machine example from Practical Model-Based Testing - A Tools Approach by Mark Utting & Bruno Legeard, pages 79 - 82. The intent of this demo is to demonstrate how a model is built for a java application and how this java program is tested from the generated test cases.

What's Included

This demo includes the following components:

  • java class to be tested that simulates vending machine:
  • MBT model (shown above) from which the test sequences are generated.
  • MBT handler which drives the java vending machine program.

Vending Machine App

The java class that simulates the vending machine is very simple, it consists of an attribute to keep track of the amount of coins (value) inserted and a set of methods to operate the machine: inserting coins, canceling and discharge the drink (see

If you were to write test cases to test this java class thoroughly, how many test cases do you have to write? Here we will focus on how to unit test this same problem using Model-Based Testing with TestOptimal.


First we build a model to describe the behaviour of this java class. The model is quite straight forward. Each state represents the amount of coins in the vending machine and a special state “Vending” to indicate enough coins are in the vending machine and it's ready to discharge the drink. See the model above.

For example at the “Start” state, the system has a ballance of 0 coins and from this state, we can insert a quarter coin, half-dollar coin or request cancel. If we choose insert a quarter coin transition, then our vending machine enters OneQuarter state. From OneQuarter state, we have again three choices: add one quarter, one half-dollor or cancel the transition.

Test Generation

To create the test sequence to tested all transitions, we just need to follow the graph with a pencil until all states and transitions are covered. But when you are done, you might wonder if you could find a shorter path if you were to try it again. We will show you a better way than tracing the graph with a pencil.

With a click on the run button, the system can generate the shortest test sequence covering all transitions in the model using a mathematical algorithm (Chinese Postman Problem). The result would look like this sequence graph Compare the system generated sequence with the one you've generated with a pencil and paper.

Automation Scripting

Now with test sequence generated, we are ready to write some code to execute it. To do this, we create an MBT handler java class implementing com.webmbt.plugin.PluginHandlerIntf, like

You might notice that each method in the handler class corresponds to a transition in the model. For example the method OneQuarter_AddHalfDollar_action is for the transition “AddHalfDollor” in state “OneQuarter” and will be called when that transition is triggered. So, we just need to implement these methods with some java code to call the vending machien methods.

At this point, we have built everything we need: a model and MBT handler (tesing script).

Execute Model

To run the test, click on the run button. The system first generates the optimal test sequence and then calls vending machine java methods for each transition on the path.

While the model is executing, use Monitor tab, Sequence Log tab and Stats tab to check for the execution status. You should see something like these:

QR Code
QR Code vendingmachine (generated for current page)