MUnit for Java Programmers: Test fixtures

February 26 2020

0 comments

Test fixtures, also known as a test context, sets up the conditions for a test to run. These test conditions will be a known good state that is specific to the code under test. When the test is completed, the state is restored or torn down. Conditions can be set up before a test suite executes and before a test runs. Test suites are extended further by parameterizing executions, enabling the same test to run different inputs. 

Test fixtures

A test suite groups tests together and executes them as a batch. This organization allows test groups to be defined by themes and test suites to be executed on a subset of the application’s functionality. 

In Java, a test suite is set up with class-level annotations that references test classes. The configurations before and after each test are defined by methods annotated retrospectively, @Before and @After.

Fig 1: Junit test suite with setup and teardown.

An MUnit test suite is a collection of MUnit tests. An unlimited number of test suites can be created as needed. The test suite defines setup and tear down logic that executes before and after the suite of tests execute. Optionally, each test within the test suite, can configure a pre and post states which are set up and torn down for each test execution. The configuration element munit:config defines a MUnit suite file.

Fig 2: MUnit suite and test setup and teardown. 

Parameterized tests

Parameterized tests are run with a set of parameters required to pass the test and are executed as many times as there are parameters defined. This allows the same test to be performed for a collection of different values.

In JUnit, an array of data is defined as the source of the parameters. When the test is executed, one of each value from the array is passed to the unit test for each execution. 

Fig 3: The String array defines the parameters passed to each test execution.

One difference between MUnit and JUnit is the ability to define an expected output value for use in the test assertion. For MUnit, test suite parameterization is defined at a configuration level. The input values are defined as attributes to the <munit:parameter> tag. This is also defined as an attribute of the <munit:parameter> tag.

Fig 4: Parameterization configuration. 

Fig 5: Parameter passed via ${input} and expected output via ${output}.

Alternatively, the list of parameters can be set in the MUnit Configuration Global Element. This global element is created automatically for each MUnit test suit. 

Fig 6: Configure the test parameters using the MUnit Configuration GUI.

Conclusion

MUnit provides the developer with the capability to construct test fixtures and parameterize tests with a similar logical approach that is familiar to Java programmers undertaking application testing

If you are interested in learning more about MUnit and DevOps related activities essential for the implementation of production-ready Mule applications then consider our course Anypoint Platform Development: Production-Ready Development Practices (Mule 4).



We'd love to hear your opinion on this post