December 18, 2014
Hot Topics:

Automated Unit Testing Frameworks

  • June 23, 2004
  • By Yasser EL-Manzalawy
  • Send Email »
  • More Articles »

Step 6: Write Tests

In JUnit, each test is a public void method with no parameters. The following two tests will be used to test the size and the enqueue () methods.

public void testSize() {
   assertEquals(queue.size(),3);
   assertEquals(empty_queue.size(), 0);
}

public void testDequeue() throws EmptyQueueException {
   int size = queue.size();

   try {
        empty_queue.dequeue();
   } catch (EmptyQueueException e) {
        assertTrue (true);
   }

   Integer i = (Integer) queue.dequeue();
   assertEquals (i, new Integer (1));
   assertEquals ("size not decremented",queue.size(), size - 1);
}

The two tests are self-explanatory; you may download the complete source available at the end of this article, examine the remaining tests, modify the tests, and follow the last step to run the tests.

Step 7: Run the Test Suite

To run your tests using the command line test runner, type:

Java TestQueue

To run the JUnit Swing test runner, type:

java junit.swingui.TestRunner TestQueue1

JUnit Test Runner

Figure 1: JUnit Test Runner

JUnit Extensions

One of the main design goals of JUnit is to be simple and fast. Due to these requirements, some features were not supported by JUnit. For example, arrays equality and accessing the private section of an object were not supported.

Because it's a framework, JUnit is extensible. Anyone can easily extend JUnit to incorporate new features. Many frameworks that extend the functionality of JUnit or just decorate it are available for free as open source projects. Table 1 summarizes some of these frameworks.

Table 1: Some JUnit Extensions
Framework Description
Cactus A simple test framework for unit testing server-side java code (Servlets, EJBs, Tag Libs, Filters, ...).
HTTPUnit Provides an easy way to write automated functional tests for Web sites, regardless of the technology used to implement them.
JunitPerf A collection of JUnit test decorators used to measure the performance and scalability of functionality contained within existing JUnit tests.
xmlUnit Allows you to compare XML documents and strings.
EasyMock A framework for dynamically creating mock object at runtime.

Source Code

Download the accompanying source code here.

Acknowledgements

The author would like to thank Rosemarie Graham for suggesting the idea of this series and Developer.com for publishing the series.

About the Author

Yasser EL-Manzalawy has been a Java programmer and instructor since 1998. He is currently an assistant Lecturer at the Systems & Computers Engineering Department, AZHAR University, Cairo. His Ph.D. research project aims to extend the Java language for agent-based systems.

Related Links

www.junit.org: the official junit site where you can freely download junit and check a large number of articles, books, and related documents.

"Top 12 Reasons to Write Unit Tests," by Eric M. Burke and Brian M. Coyner.

"JUnit best practices," by Andy Schneider.

"JUnit and Its Extensions Make Unit Testing Your Java Code Much Easier," by Bryan Dollery.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel