The ModelJUnit Model-Based Testing Tool

Authors: Dr Mark Utting, Gian Perrone, Jerramy Winchester,
Scott Thompson, Rong Yang and Pele Douangsavanh.
From: Department of Computer Science, The University of Waikato, New Zealand.
Sourceforge Site: http://sourceforge.net/projects/modeljunit

ModelJUnit is a Java library that extends JUnit to support model-based testing. Models are extended finite state machines that are written in a familiar and expressive language: Java. ModelJUnit is an open source tool, released under the GNU GPL license.

ModelJUnit allows you to write simple finite state machine (FSM) models or extended finite state machine (EFSM) models as Java classes, then generate tests from those models and measure various model coverage metrics. The principles behind ModelJUnit are described in Sections 5.2 and 5.3 of our book, Practical Model-Based Testing. It is also possible to use ModelJUnit from within the Groovy language, as some Groovy person shows here.

Latest Version of ModelJUnit

Below is release 2.0 beta1 of ModelJUnit (13 May 2009). This version includes a prototype version of a new GUI for generating tests, with better visualization of models.

Note that ModelJUnit uses the annotations feature of Java 5.0, and this binary release was compiled with JDK1.6, so requires JDK 1.6 or higher to run.

Documentation and Introductory Example

Version 1.0 of ModelJUnit was presented in a Google Tech Talk by Mark Utting in August 2007. The first 30 minutes of the talk are an introduction to model-based testing, ModelJUnit is discussed from 28:20 onwards and a more sophisticated commercial tool, LEIRIOS Test Designer (now called Smartesting Test Designer), is discussed from 43:50 onwards.

Here is an example model, FSM.java, which shows how you can write the following simple FSM model in the ModelJUnit style.

[FSM model with 3 states: s0, s1, s2]

We can easily generate some tests from this model, by executing the Java code:

    Tester tester = new RandomTester( new FSM() );
    tester.addListener("verbose"); // print the tests
    tester.generate(20); // generate a sequence of 20 random tests

The main method of the FSM class shows a more sophisticated version of this, which also measures the model coverage metrics of the generated test suite. It produces this output. You can run this main method as follows (you must have modeljunit.jar and junit.jar in your classpath and use Java 1.6):

    java nz.ac.waikato.modeljunit.examples.FSM

This example just prints messages as the model is executed. The generated messages could be saved in a file and used as a test script for later test execution (offline testing). But we usually use ModelJUnit for online testing, where the tests are executed on the system under test (SUT) as they are generated.

To do online testing, the test generation code within the above main method is usually written within the TestXYZ() methods of your JUnit test classes, so that each time you run your JUnit test suite, you will generate a suite of tests from your FSM model. Also, the @Action... methods in your model class will include code to call the methods of your SUT, check their return value, and check the status of the SUT. In this way, every time you run your JUnit tests, the model is used to generate a sequence of @Action... calls and test your SUT.

A new and experimental feature of ModelJUnit is a GUI interface, which allows you to generate tests by pointing and clicking, or to generate the code that generates the tests. To run the GUI, you can just double-click on the Modeljunit .jar file, or put it in your CLASSPATH, then run:

    java nz.ac.waikato.modeljunit.gui.Main

Feedback welcome!

Related Work

Other similar open-source model-based testing tools include:
marku@cs.waikato.ac.nz
Last modified: Fri May 15 18:05:54 NZST 2009