STRUTS 2 Unit Test instance

Source: Internet
Author: User

When a programmer writes a complete program, does he or she ensure that the program is correct? Of course, to test, programmers usually write a program to test the correctness of the target program. Such tests are programmer tests, known as white-box testing, because programmers know how the software being tested accomplishes functions and what (What) functions are done. This programmer-based test is called unit testing.

Unit Test principle

The principle of unit testing is actually very simple. Is the consistency of the test expectation and the actual results under certain conditions. For example, we write Java classes as shown in Listing 2.1 of the program, which has a method sum.

Package com.kingbegin; 
     
public class A {public 
    int sum () {return 
        8; 
    } 
}

We test the sum method of this class, we can do a test class, instantiate a class in the test class and call its sum method, if it actually returns the value and we expect the value (8) Consistent with the words that this class is written without problems, otherwise there will be errors. The test program is shown in Listing 2.2.

Package ch01; 
Package com.kingbegin; 
     
public class TestA1 {public 
     
    static void Main (string[] args) { 
        A A = new A (); 
        if (a.sum () = = 8) { 
            System.out.println ("Test results are correct!") "); 
        } else { 
            System.out.println ("The test result is incorrect!") "); 
        } 
    } 
     
}

So we run the test program, we can test whether the test program is correct or not.

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.