Java Unit Testing - JUnit & TestNGJUnit, created anaboliset steroidit Kent Beck and Erich Gamma, is an open source framework for test-driven development in any Java-based code. JUnit automates unit testing and reduces anabolicke tablety effort required to frequently test code while developing it. While there are lots of bits of documentation all over the place, there isn't a go-to-manual that serves as a quick reference for JUnit. This Pocket Guide meets the need, double epsilon junit together all the bits of hard to remember information, syntax, and rules for working with JUnit, as double epsilon junit as delivering the insight and sage advice epsilno can only come from a technology's creator. Any programmer who has written, or is epsklon, Java Code will find this book valuable.
Assert (JUnit API)
Prior to the arrival of "unit testing framework", programmers tends to write test expressions which print to the console or a trace file the amount of output is sometimes controlled by a trace-level or debug-level flag.
This approach is not satisfactory because it requires human judgment to analyze the results produced. Too many print statements cause the dreaded Scroll Blindness. Nonetheless, assertion is primitive compared with the unit testing framework. With a proper Unit Testing framework, you can automate the entire unit testing process. Your job becomes designing proper test cases to excite the program.
Furthermore, the unit testing process can be integrated into the build process. In this case, the build process not only checks for syntax errors, but semantic errors as well. It is the de facto standard for Java Unit Testing. You could download the API documentation as well as the source code. To use the JUnit, include JUnit jar-files " junit Create Test case or Test Suite: Run Test case or Test Suite: There are two versions of JUnit, version 3 and version 4, which are radically different.
JUnit 4 uses the annotation feature since JDK 1. Suppose that we wish to carry out unit testing on the following Java program, which uses static methods to perform arithmetic operations on two integers. Take note that divide throws an IllegalArgumentException for divisor of zero. You can include more than one test cases using runClasses test1. JUnit also provides a console version of test-runner called org. JUnitCore for you to run the tests in command-line, with the following syntax:.
Let's write another test case to test the divide methods, which throw an Exception for divisor of zero.
Furthermore, the method divReal returns a double which cannot be compared with absolute precision. Run the test and observe the test result. Change testDivRealPass 's expected value from 0. Take note that the test suite class is marked by annotation RunWith and SuiteClasses with an empty class body.
Observe the test results produced. You can also run the test suite via Test Runner org. JUnitCore , just like running test cases as described earlier. Instead of testing static methods in a Java class, let's carry out unit test on a proper self-contained and encapsulated Java class with its own private variables and public operations. Suppose that we have a class called MyNumber that represents a number, and capable of performing arithmetic operations.
A test fixtures is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. We typically declare text fixtures as private instance variables; initialize via Before annotated method; and clean-up via After annotation method. Each test method runs on its own set of text fixtures with the same initial states.
This ensures isolation between the test methods. JUnit 4 introduces parameterized test which allows you to run the same test over and over again using different values.
To use parameterized test:. The output trace suggests that Parameters method is run once. To run the test, you can either include a main method as above, or use the command-line. I suggest that you move to JUnit 4, which is more intuitive by using annotation. The unit-test program using JUnit framework is as follows.
Black-box test cases are set up to test typical values as well as boundary values. Note that one of the unit-test cases catches the logical error. If two or more test methods use a common set of test objects called test fixture s , declare the test fixtures as instance variables.
For example, suppose we are testing the class ArrayList. Initialize the text fixture. You can override setUp or use the constructor. Each test method runs on its own TestCase instance.
This provides isolation between test methods. Each test method invoke the constructor to construct an instance of the TestCase , followed by setUp , run the steps coded inside the test method, and the tearDown before exiting. The test methods may run concurrently. For example, let's initialize our test fixture ArrayList with two String elements. Write the test methods for this TestCase. All the test methods must be named testXxx , as JUnit uses reflection to find these test methods.
There are two versions of TestRunner: TestRunner , and GUI-based junit. To use the text-based TestRunner , you could include a main method as follows:. If there are many TestCase s could be written by different people , you can put them together into a TestSuite and run all the TestCase s at once.
To do so, in each of the TestCase s, create a static method suite to extract all the test methods as follows:. Apache's ANT is the de facto standard for automated building of Java applications similar to Unix's " make " utility. You can download ANT from ant. We shall use ANT to automate building and testing. First, create a configuration file called "build. To build using the above build file, run " ant ".
By default, it executes " ant run ", which is depends on " build ", " build " get executed to compile the program, then " run " get expected to run the testing. To run only the compilation, use " ant build ". To run only the cleanup, use " ant clean ". How to test a program to ensure correctly? There are two techniques: White-box testing inspect the program codes and test the program logic. Black-box testing does not inspect the program codes, but looking at the input-output, treating the program as a black box.
For black-box testing, the most common approach is to partition the inputs, and design test cases for each input partition. The test cases could test on a typical input value as well as the boundaries. For example, the following program converts a given mark to a letter grade 'A' to 'F'. There is a logical error in the program. Try to run the above tests to find the logical error. Take note that assertEquals does not accept char as arguments, but upcast to long. That is, the output show the char 's numeric code.
From TestNG download site http: Unzip the downloaded file. The binaries is kept in " testng Prepare the following XML Description file says " testing. Each test case comprises of many Java classes. JUnit 4 uses an dedicated annotation Ignore to override the Test annotation. The values will be passed into the arguments of the method in the same order. The Parameters annotation can only used to pass simple type such as String and int. To pass objects, you need to use DataProvider annotation.
In method1 , if we change to assertTrue false to fail the test, method2 and method3 will not be run, but marked as skip instead of fail as in JUnit 4 , as shown in the following outputs:. Each test method can be assigned to one or more groups.
We can select one or more groups to test via XML description file. Instead of specifying dependency on individual method names as in the previous section, we can place related method e. If we use assertTrue true in method1 , method3 will be run. However, if we use assertTrue false in method1 , method3 will be skipped. Introduction to Unit Testing Framework The various type of software testings include: Acceptance Test or Functional Test: Tests to ensure the a change such as enhancement, patches or configuration change does not break the system or introduce new faults.
Extreme Programming Extreme programming www. The xUnit architecture has these common components: JUnit 4 There are two versions of JUnit, version 3 and version 4, which are radically different.