JUnit Extensions - JUnit

What are Junit extensions?

Let’s see following JUnit extensions −
  • Cactus
  • JWebUnit
  • XMLUnit
  • MockObject

What is Cactus?

Cactus is a simple test framework used for unit testing server-side java code (Servlets, EJBs, Tag Libs, Filters). Cactus is used to reduce the cost of writing tests for server-side code. Cactus uses JUnit and extends it. It implements an in-container strategy that executes the tests inside a container.
Cactus ecosystem includes following components −
  • Cactus Framework is the heart of Cactus. It is the engine that provides the API to write Cactus tests.
  • Cactus Integration Modules are front-ends and frameworks that provide easy ways of using the Cactus Framework (Ant scripts, Eclipse plugin, and Maven plugin).
Here’s the code that demonstrates how Cactus can be used.

What is JWebUnit?

JWebUnit is used for web applications as it is a Java-based testing framework. It combines with the existing testing frameworks like HtmlUnit and Selenium with a unified, simple testing interface to test the correctness of your web applications.
JWebUnit offers a high-level Java API to navigate a web application in combination with a set of assertions to check the application's correctness. It includes navigation across links, form entry and submission, validation of table contents, and other typical business web application features.
JWebunit allows the simple navigation methods and ready-to-use assertions to speed up the test creation than using only JUnit or HtmlUnit. Incase if you want to switch from HtmlUnit to other plugins like Selenium (available soon), then no need to rewrite your tests.
Here is a sample code.

What is XMLUnit?

XMLUnit includes a single JUnit extension class, XMLTestCase, and a set of supporting classes that allow assertions to be made about −
  • The differences between two pieces of XML (via Diff and DetailedDiff classes).
  • The validity of a piece of XML (via Validator class).
  • The outcome of transforming a piece of XML using XSLT (via Transform class).
  • The evaluation of an XPath expression on a piece of XML (via classes implementing the XpathEngine interface).
  • Individual nodes in a piece of XML that are exposed by DOM Traversal (via NodeTest class).
Let’s see the two pieces of XML to compare and assert that they are equal. Following simple test class explains this:


Mock objects explains the behavior of complex, real (non-mock) objects which are useful when a real object is impractical or impossible to incorporate into a unit test.
The common coding style for testing with mock objects is to −
  • To create instances of mock objects.
  • To set state and expectations in the mock objects.
  • To invoke domain code with mock objects as parameters.
  • To verify consistency in the mock objects.
Let’s see an example of MockObject using Jmock.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JUnit Topics