|Authors:||Dr Mark Utting,
Rong Yang, Pele Douangsavanh, Celia Lai, Julian Utting.
|From:||Department of Computer Science, The University of Waikato, New Zealand.|
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.
The latest version of ModelJUnit is available for download from the Sourceforge development site.
Alternatively, you can check out the latest sources of
ModelJUnit using the following SVN command. Then look in
for examples of using ModelJUnit.
svn checkout svn://svn.code.sf.net/p/modeljunit/code/trunk modeljunit-code
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 CertifyIt), 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.
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
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
You can run this main method as follows (you must have
modeljunit.jar and junit.jar in your classpath):
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
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
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: