Testing an application with JUnit
You are now ready to test your jn_test application. To test, you also need to use JUnit Wizard to create a new class to extend the JUnit test cases. To use this wizard, right-click on the Jn_test in Package Explorer and select New->other to open a new dialog box, as shown in the figure:
Now expand the Java node and select JUnit, then select the JUnit Test case and click the Next button, as shown in figure:
Typically, the JUnit class names the name you want to test with and adds test after it. So named for Jn_testtest. Alternatively, select the Setup and Teardown methods that establish and clean up data and/or objects in test cases (their terminology in junit is fixtures). After filling in the image above, click Next. As shown in figure:
In this dialog box, select the method you want to test so that JUnit Wizard can create stubs for them. Because we want to test allocate,see and get, select them, and click the Finish button to create Jn_testtest class. As follows: A method stub is created for the Allocate,set and get methods in Jn_testtest class: Testallocate, Testset, and Testget.
Package net.csdn.blog;
Import Junit.framework.TestCase;
public class Jn_testtest extends TestCase {
/*
* @see Testcase#setup ()
*/
protected void SetUp () throws Exception {
Super.setup ();
}
/*
* @see Testcase#teardown ()
*/
protected void teardown () throws Exception {
Super.teardown ();
}
public void Testallocate () {
}
public void Testget () {
}
public void Testset () {
}
}
The next step is to add code to these stubs to call the Allocate,set and get methods in the Jn_test class so that you can use the JUnit assertion method for the results. We will need an object of the Jn_test class to call these methods and name them testobject. To create Testobject use the Setup method in the JUnit code. This method is called before the JUnit test starts, so we will create Testobject from the Jn_test class that will be tested.
Jn_test Testobject;
.
.
.
protected void SetUp () throws Exception {
Super.setup ();
Testobject = new Jn_test ();
}
Now you can use this object for testing. For example, the allocate method is used to create an integer array and return the array, so use the Assertnotnull test in testallocate and make sure that the array is not empty:
public void Testallocate () {
Assertnotnull (Testobject.allocate ());
}
The Get method is supposed to retrieve a value from the array and so we can test as using assertequals with a test V Alue in Testget:
The Get method takes a numeric value from the array and tests the method with Assertequals in Testget.
public void Testget () {
Assertequals (Testobject.get (1), 1);
}
And the set method are supposed to return true if it's been successful, so we can test it with asserttrue as this
The Set method returns True at a successful time, using Asserttrue to test it.
public void Testset () {
Asserttrue (Testobject.set (2,4));
}
After adding the code, select Jn_testtest in Package Explorer and select the Run As->junit Test menu item, as shown in the figure:
The above illustration shows that there are errors, in the JUnit view, three Tests have been forked, indicating that the test failed, we first check the first test, testallocate, it tests that the created array cannot be null:
public void Testallocate () {
Assertnotnull (Testobject.allocate ());
}
Look at the first trace: Oh, this place is missing. To create an array, we fix the code:
Public int[] Allocate ()
{
Array = new INT[3];
Array[0] = 0;
ARRAY[1] = 1;
ARRAY[2] = 2;
return array;
}
Now run Jn_testtest again, now only Testget and testset two errors, as shown in figure:
What's wrong with Testget and Testset? The problem here is that all JUnit tests are run separately. This means that although the first Test testallocate called the Allocate method, it was not invoked by the next two test testget and Testset. So in order to initialize the arrays for both tests, the allocate method must be called in both Testget and Testset. Add the following code:
public void Testget () {
Testobject.allocate ();
Assertequals (Testobject.get (1), 1);
}
public void Testset () {
Testobject.allocate ();
Asserttrue (Testobject.set (2,4));
}
Run the test now, all through. The menu bar at the top of the JUnit view is green, as shown in the figure:
As shown above, JUnit provides a fairly simple way to create a standard set of tests that we can do with just a few mouse clicks. As long as the test is created, you only need to run the JUnit class you created.
However, it should be noted that JUnit only uses a set of tests to verify compatibility, and if there is a problem in your code, or you don't know what to do, you need to debug. (End of full text)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.