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.

You can check the test cases generated in SeqTrace.

With the model completed, we are ready write the java handler to drive the java application.


Generate Skeleton Code

The java handler is just a plain java class implementing com.webmbt.plugin.PluginHandlerIntf with a set of methods to be called by the model transitions.

First we let TestOptimal generate the java handler skeleton code from the model. This is done by going to the Model Property and click on Generate link next to the Java Class field.

Copy and paste the skeleton java code to the new java handler class, make sure the class implements com.webmbt.plugin.PluginHandlerIntf and JavaPlugin enabled for the model.


Implement Test Methods

There are many methods in the skeleton code but we only need to implement a few of the methods for our example. Below is a list of code components we need to add to this handler class:

  1. create an instance variable in this class to store our VendingMachine application object: private VendingMachine vendMachine;
  2. implement MBT_start() to create our vending machine object: this.vendMachine = new VendingMachien();
  3. implement all of the *_action transition methods by placing one of the following java code to each method accordingly:
    • this.vendMachine.addQuarter();
    • this.vendMachine.addHalfDollar();
    • this.vendMachine.cancek();
  4. implement the *_onentry state methods to verify that we are at the right state by placing the following code to the method:
    • if (this.vendMachine.getSum()!=nn) throw new MBTException (“L1: incorrect amount/state, expecting nn but got ” + this.vendingMachine.getSum());

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.