Friday 17 January 2014

Testing Using JUnit


TestingUsing JUnit:
While compilers can look for structural problems in a program, they cannot tell whether the results of a program or method are correct. Instead, all developers test their programs to ensure that they behave as expected.
This can be as simple as calling a method in the Interactions Pane to view its results, but this technique requires you to think about the answers you expect every time you run any tests. A much better solution is to give the tests the answers you expect, and let the tests themselves do all the work.
a technique known as unit testing makes this quite easy. You write many small tests that create your objects and assert that they behave the way you expect in different situations. A unit test framework known as JUnit automates the process of running these tests, letting you quickly see whether your program returns the results you expect.
Java makes the process of running unit tests very simple by providing support for JUnit. Once you have written a JUnit test class (as described in the next section), you can simply choose the "Test Current Document" command from the Tools menu to run the tests and view the results.
Also, clicking the "Test" button on the toolbar or choosing "Test All Documents" from the Tools menu will run JUnit on any open testcases, making running multiple test files very simple.
JUnit is a framework for developing unit tests for Java classes. JUnit provides a base class called TestCase that can be extended to create a series of tests for the class you are creating, an assertion library used for evaluating the results of individual tests, and several applications that run the tests you create.

With the JUnit framework, unit tests are any public classes that extend the junit.framework.TestCase class, and that have any number of methods with names beginning with the word "test". JUnit provides methods to easily assert things about your own classes, as well as the ability to run a group of tests.

The requirements for writing unit test classes are described below, with an example provided in the next section. In general, though, the intent is for you to create instances of your classes in the test methods, get results from any methods that you call, and assert that those results match your expectations. If these assertions pass, then your program has behaved correctly and your tests have succeeded.

Writing a Test Case. To use  Java's Test command on a document, you must use the programming conventions outlined below. You can also choose the "New JUnit Test Case" command from the File menu to automatically generate a template with these conventions.
At the top of the file, include:

import junit.framework.TestCase;
The main class of the file must:

be public

extend TestCase


be public and not static

return void

take no arguments

have a name beginning with "test"


void assertTrue(String, boolean)
which issues an error report with the given string if the boolean is false.

void assertEquals(String, int, int)
which issues an error report with the given string if the two integers are not equal. The first int is the expected value, and the second int is the actual (tested) value. Note that this method can also be called using any primitives or with Objects, using their equals() methods for comparison.

void fail(String)

Test methods are permitted to throw any type of exception, as long as it is declared in the "throws" clause of the method contract. If an exception is thrown, the test fails immediately.

If there is any common setup work to be done before running each test (such as initializing instance variables), do it in the body of a method with the following contract:

This method is automatically run before any tests in the class. (Similarly, you can write a protected void tearDown() method to be called after each test.)

If you would rather control which methods are called when running the tests (rather than using all methods starting with "test"), you can write a method to create a test suite. This method should be of the form:

public static Test suite() {
  TestSuite suite = new TestSuite();
  suite.addTest(new <classname>("<methodname>"));
  ...
  return suite;
}
It is then also necessary to import TestSuite and Test from junit.framework. There is also a version of the addTest method that takes a Test, so test suites can be composed.


No comments:

Post a Comment