Java Unit Test (JUNIT)

Source: Internet
Author: User

JUnit is a regression testing framework (regression testing framework) written by Erich Gamma and Kent Beck for Java developers to write unit tests. JUnit has different techniques for using objects of different natures, such as CLASS,JSP,SERVLET,EJB. The following is an example of a class test.

Test steps:

1 Importing JUNIT4-related jar packages

(Select Java Project, right-click---> select Properties----> Java Build Path----in the window > click Add Library on the right----> In the pop-up window list, select JUnit----> Next----->junit 4 (I'm using Junit 4)---->finish)

2 can be tested in the class new test class, select Setup,next to test the method, the system will automatically generate the corresponding test method, modify the corresponding method content.

You can also click src---> Right--->new--->test Junit case

Write the test method in the testing method.

You can also customize the test method, but add @test annotations, the method format is public void Testxxx () throws exception{}

For example:

@Test
public void Testadd () throws exception{
}

3 Modifying the test class (first the instance of the class being tested, and then a single test of the Method (Eg:assertequals (8, Calculator.getresult ())))

4 Run as JUnit test (you can display the Outline window, select the test class you want to test, and in outline select the method you want to test right-click Run as Junit test)

code example:

The class being tested:

public class Calculate {
Addition operation
public int sum (int x,int y) {
return x+y;
}
Subtraction operations
public int sub (int x,int y) {
return x-y;
}
}

Test class:

Import static org.junit.assert.*;
Import Junit.framework.TestCase;
Import Org.junit.Test;

public class Calculatetest extends testcase{

Calculate Calculate = new Calculate ();//First new instance object of the class being tested
@Override
protected void SetUp () throws Exception {
TODO auto-generated Method Stub
Super.setup ();
}

@Override
protected void TearDown () throws Exception {
TODO auto-generated Method Stub
Super.teardown ();
}

@Test
public void Testsum () {
Assertequals ("Test Addition", 4, Calculate.sum (1, 3));//test
}

@Test
public void TestSub () {
Assertequals ("Test Subtraction", 2, Calculate.sub (4, 3));//test
}

}

We are interested in in-depth research and can understand:

JUnit itself is designed around two design patterns: the command mode (junit test case) and the Integrated mode (JUnit test Suite).

Simply put: The JUnit test case is a single method test. The JUnit test suite is a collection (which can include multiple test CAs and test Suite) in the collection.

The code example is as follows:

Test Case individual tests:

For example, we want to test the money's Add method, which can be as follows:
public class Moneytest extends TestCase {//testcase Subclass
public void Testadd () {//Put the test code in Testadd
Money m12chf= New Money ("CHF"); The bank and the next line do some initialization
Money m14chf= New Money ("CHF");
Money expected= New Money ("CHF");//Expected results
Money result= M12chf.add (M14CHF); Run the method being tested
Assert.asserttrue (expected.equals (result)); Determine if the running result is the same as expected
}
}

If you test the Equals method, use a similar code, as follows:
public class Moneytest extends TestCase {//testcase Subclass
public void Testequals () {//Put the test code in Testequals
Money m12chf= New Money ("CHF"); The bank and the next line do some initialization
Money m14chf= New Money ("CHF");

Assert.asserttrue (!m12chf.equals (null));//test for different situations
Assert.assertequals (M12CHF, M12CHF);
Assert.assertequals (M12CHF, New Money ("CHF")); (1)
Assert.asserttrue (!m12chf.equals (M14CHF));
}
}


When you want to test both the add and the Equals methods, you can combine their own initialization work together to form a test base, initialize with Setup, and clear with teardown. As follows:
public class Moneytest extends TestCase {//testcase Subclass
Private money f12chf;//fetching common objects
Private Money F14CHF;

protected void SetUp () {//Initialize common objects
f12chf= New Money ("CHF");
f14chf= New Money ("CHF");
}
public void Testequals () {//test correctness of the Equals method
Assert.asserttrue (!f12chf.equals (null));
Assert.assertequals (F12CHF, F12CHF);
Assert.assertequals (F12CHF, New Money ("CHF"));
Assert.asserttrue (!f12chf.equals (F14CHF));
}

public void Testsimpleadd () {//test the correctness of the Add method
Money expected= New Money ("CHF");
Money result= F12chf.add (F14CHF);
Assert.asserttrue (expected.equals (result));
}
}

Test Suite Collection tests:

public class Moneytest extends TestCase {//testcase Subclass
....
public static test suite () {//Static test
TestSuite suite= new TestSuite ();//Generate a TestSuite
Suite.addtest (New Moneytest ("Testequals")); Adding Test methods
Suite.addtest (New Moneytest ("Testsimpleadd"));

Suite.addtest (New Moneytest (Moneytest.class)); Take class as Parameter
return suite;
}
}

Java Unit Test (JUNIT)

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.