JUnit Test Notes

Source: Internet
Author: User
Tags relative

Today we talk about JUnit testing techniques, which most people have heard, it's just that some people don't use it, it's easy to say, but it's really an essential technology in the software development process, for some irregular small companies maybe the product release is not tested, but for large software development, This technology is often essential, because any error in a small place can be difficult to find, but the JUnit Technology Unit testing technology allows us to develop side tests that make our final product less likely to go wrong.

The example I'm talking about today, the development environment is myeclipse6.0, in this development environment has actually integrated the JUnit test related classes that we want, as long as we set up in the environment, as shown in the following figure, when we build a Java project, click on the project properties, as shown in the following image set:

At this point our project is bringing in the classes that are related to these junit tests, and we can start our testing process.

We usually write our source code under the SRC source file package, in order to ensure that our test class can be separated from our project and that the test class of our project can be compiled in the same package with the compiled files of the class we are testing, we can create a new test source package in the project. But we're going to make sure that the test class has the same package path as the class we're testing, for example, the relative path of our test class is org.rjb.Calculate, then the path to the test class that we've tested in the source package is org.rjb.CalculateTest. So they compile and then under the same package path, do not believe you can try Oh.

Let me start with some rules:

The test class noun we can define to be named by the test class name +test (unwritten stipulation);

The test class must inherit from Junit.framework.TestCase, which has several methods to declare here:

1.public void SetUp (): This method is the method to be invoked before each test method is started, that is, there are several methods to test, that is, a few calls are made, only in JUnit3.8, but only once in the JUnit4.0.

2.public void Teardown (): This method is relative to the above method, which is called after each test method test, and the number of calls is exactly the same as the above method.

The method naming rule in the test class is the test+ test method name, which is, of course, capitalized by the test method name. That means that the test method must start with test, and it is visible that the corresponding method is found by reflection.

Given that we can execute all of the test classes at once, we can define a test class, as long as we inherit junit.framework.TestSuite, we must have the following method, the public static test suite (), How exactly to use please see the code shown below.

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.