Eclipse Learning 4 - Unit Test in Eclipse (below)

xiaoxiao2021-03-05  23

Test an application using JUnit

It is now ready to test the JN_Test application. For testing, you also need to create a new Class to extend the JUnit test case using JUnit Wizard. To use this Wizard, right-click on the jn_test in the package explorer, and select New-> Other to open a new dialog, as shown in the figure:

Expand the JAVA node and select JUnit, then select JUnit Test Case, click Next button, as shown:

Normally, the JUnit class named and the class named by the class it is tested, and TEST is added later. So named JN_TestTest. Alternatively, the SETUP and TEARDOWN methods are selected, which establish and clean up the data and / or objects in the test case (, they are fixtures in JUnit). Follow the figure above, click Next. As shown in this dialog box, select the method you want to test, so JUnit Wizard can create a stub (stub) for them. Because we want to test allocate, see, and get, select them, and click the Finish button to create a JN_TestTest Class. As shown below: A method stub is created in the JN_TestTest Class to create a researcher: Testallocate, TestSet, and Testget. Package Net.9cbs.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 settles to make them call the Allocate, SET and GET methods in the JN_TEST class, so that the results can be used to use the JUnit assertion method. We will need an object of a JN_TEST class to call these methods, name it TestObject. To create TestObject using the setup method in the JUnit code. This method is called before the JUnit test begins 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 ();

}

This object can now be tested. For example, the allocate method is used to create a integer array and return this array, so use AssertNotnull test in Testallocate and confident that this array is not empty:

Public void testallocate () {

askERTNOTNULL (TestObject.allocate ());

}

The Get Method Is Supposed to Retrieve A Value from the Array, So We CAN Test That Method Using Assertequals WITHD VALUE IN TESTGET: The GET method acquires the value from the array, and this method is tested with Assertequals in the TestGet.

Public void testget () {

Assertequals (TestObject.get (1), 1);

}

And the set method is supposed to return true if it's been surcessful, sowe can Test It with aski

The SET method returns True when successful, uses asserttrue to test it.

Public void testset () {

AssertTrue (TestObject.set);

}

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 This is an error. In the JUnit view, three tests have been hitted, indicating the test failed, let's check the first test, Testallocate, which test is the created array can't be null:

Public void testallocate () {

askERTNOTNULL (TestObject.allocate ());

}

Take a look at the first TRACE: Oh, this place hosted the creation array, we correct the code:

Public int [] allocate ()

{

Array = New Int [3];

Array [0] = 0;

Array [1] = 1;

Array [2] = 2;

Return array;

}

Now run JN_TestTest, now there are only two errors in Testget and TestSet, as shown in the figure:

What is the problem with Testget and TestSet? The problem here is that all JUnit tests are running separately. In other words, although the first test Testallocate calls the allocate method, it is not called the next two test TestGet and TestSet. So in order to initialize its array to these two tests, you must call allocate methods in 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);

}

Now run test, 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 set of standard tests, we only need to move a few mouses. As long as the test is created, you only need to run the JUnit class you created.

However, it is necessary to note that Junit is only tested with a set of tests to verify compatibility. If there is a problem in your code, or you don't know what to do, you need to perform Debug. (Full text)

转载请注明原文地址:https://www.9cbs.com/read-34981.html

New Post(0)