Test with JUnit
JUnit is an open source test framework provided with Eclipse. In the same project, you can create "JUnit" Class with other Class, and use this JUnit code to test other Class in Project. This use of JUnit can build a set of standard tests for those workers working on this application. If you change the application code, the work they need is just a click of the button to verify that this application is still able to test.
JUnit is used to test the code, and it is composed of an assertion method that can test different conditions, as shown below:
Assertequals (A, B)
Testing A is equal to B (A and B are primitive value) or must be equally equally equally equivalent)
askERTFALSE (A)
Testing whether a is false (fake), A is a Boolean value.
AssertNotnull (a)
Testing whether A is non-empty, A is an object or null.
AssertNotsame (a, b)
Test whether A and B have not reference the same object.
askERTNULL (A)
Testing whether A is NULL, A is an object or NULL.
askERTSAME (A, B)
Testing A and B are referenced by the same object.
askSERTRUE (A)
Test A is True (true), A is a Boolean value.
We use these methods to build JUnit tests. When running a JUnit application, it opens its own view (view) to immediately indicate which test passes, which test failed.
Create a test application
We will use Junit in an example to demonstrate how it is running in the application jn_test we created. The intent of this app is simply assigned to an integer array and access them through the GET and SET methods.
In particular, we will contain three methods that are used to return values that may be tested in JUnit.
Allocate
Assign a integer array and assign it; return to the newly assigned array.
set
Assign a value for integer arrays; if you have returned true, you will return FALSE.
get
Take the value from the integer array; return the required value.
JN_TEST initial version:
Package Net.9cbs.blog;
Public Class JN_Test {
Private int [] array;
Public int [] allocate ()
{
Array [0] = 0;
Array [1] = 1;
Array [2] = 2;
Return array;
}
Public int GET (int index) {
Return array [index];
}
Public Boolean Set (INDEX, INT VALUE) {
IF (INDEX
{
Array [index] = value;
Return True;
}
Else
Return False;
}
}
Allocate method assigns an integer array to assign it, and return this array:
Private int [] array;
Public int [] allocate () {
Array [0] = 0;
Array [1] = 1;
Array [2] = 2;
Return array;
}
The GET method is taken in a given position of the array:
Public int GET (int index) {return array [index];
}
The SET method is a given value for a given position, and TRUE or FALSE is returned by the condition.
Public Boolean Set (INDEX, INT VALUE) {
IF (Index
Array [index] = value;
Return True;
} else {
Return False;
}
}
Next is to test all these methods using JUnit: Allocate, SET, and GET.
Install JUnit
In order to use JUnit (more details, please refer to http://www.junit.org), you must add JUnit.jar in the Project's classpath. Therefore, establish a new ClassPath variable JUnit. Select the Window-> Preferences menu bar to open the Preferences dialog, expand the Java node, select the build path-> ClassPath Variables item.
Then click the New button to open the New Variable Entry menu, enter the new variable name: JUnit, set the path to junit.jar. In Eclipse
In 3.0.2, you can find Junit.jar under Eclipse / Plugins / Org.junit_3.8.1 / junit.jar. Then click OK. For the need for DEBUG, you should also add the source code for the JUnit package (this is an optional step; in this example we do not use the JUnit source code). You will be in /Eclipse/plugins/org.eclipse.jdt.source_
3.0.2 / src directory found the JAR file of the source code of the source code. Create a new variable for JUnit Source Codes, follow the steps to create Juntit above, but this variable is named JUnit_SRC (usually adding a prefix _SRC in front of the source variable) and connect it to /eclipse/plugins/org.eclipse.jdt . Source_3.0.2 / src / org.junit_3.8.1 / junitsrc.zip. After creating this variable, right-click on Project JN_TEST, select Properties, open the JN_Test's Properties dialog, click Java Build Path item and libraries option. And add variable, add a JUnit variable, the resulting is shown: When expanding a JAR file node, you can specify where to find the associated source code and Javadoc. To use JUnit_SRC to use JUnit_SRC here, select Source AttacHement in the node, click Edit to open the Source Attachment Configuration dialog. Click the Variable button, double-click JUnit_SRC, and click OK to close this dialog. As shown in the figure: Here we have made JUnit's source code can be used on Eclipse. Click OK to close the JN_Test's Properties dialog. Not finished, to be renewed ............ Use Junit in Eclipse (below)