Use junit4 in eclipse for unit testing (in)

Source: Internet
Author: User
Abstract: This article continues to describe how to use junit4 in eclipse for unit testing.

The junit4 framework is used in the test class, and the corresponding package must be included. The most important package is org. JUnit .*. After it is included, most functions will be available. Another important sentence is "Import static org. JUnit. Assert. *;". The assertequals methods we use during testing come from this package. Please note that this is a static inclusion (static) and a new feature added in JDK 5. That is to say, assertequals is a series of static methods in the assert class. The general usage is assert. assertequals (), but after static inclusion is used, the previous class name can be omitted, making it easier to use.

Ii. Declaration of test classes

We noticed that our test class is an independent class without any parent class. The name of the test class can also be named without any limitations. Therefore, we can't judge whether it is a test class through the declaration of a class. The difference between it and a common class is the declaration of its internal methods. We will talk about it later.

  3. Create an object to be tested.

To test the class, you must first create an object of the class. As the code in the previous article:

private static Calculator calculator = new Calculator();

To test the calculator class, we must create a calculator object.

  Iv. Declaration of test methods

In the test class, not every method is used for testing. You must use "annotation" to specify which methods are used for testing. "Annotation" is also a new feature of jdk5. it is very appropriate to use it here. We can see that some methods are preceded by the words @ before, @ test, and @ ignore. These are annotations, starting with. These annotations are customized by junit4. It is very important to be familiar with the meanings of these annotations.

  5. Compile a simple test method.

First, use the @ test annotation before the method to indicate that this is a test method. The method declaration also has the following requirements: the name can be obtained without any restrictions, but the return value must be void and cannot have any parameters. If these rules are violated, an exception is thrown at runtime. As for what to write in the method, it depends on what you need to test. For example:

@Test
public void testAdd() ...{
calculator.add(2);
calculator.add(3);
assertEquals(5, calculator.getResult());
}

If we want to test whether the "addition" function is correct, call the Add function several times in the test method. The initial value is 0. Add 2 first and then 3, the expected result is 5. If the actual result is also 5, the add method is correct, and vice versa. Assertequals (5, calculator. getresult () is used to determine whether the expected result is equal to the actual result. Enter the expected result in the first parameter, and enter the actual result in the second parameter, that is, the calculated result. After this is done, JUnit will automatically perform the test and report the test result to the user.

  6. Ignore some unfinished methods for testing.

If you have made a good plan before writing a program, you should determine which methods and functions should be implemented. Therefore, even if the method has not been completed, its specific functions are also determined, which means you can write test cases for him. However, if you have completed the test case of this method, but this method has not been completed, the test must be "failed ".

This kind of failure is different from the real failure, so JUnit provides a way to differentiate them, that is, add @ ignore mark before the test function, this annotation indicates that "some methods have not been completed and will not participate in this test for the time being ". In this way, the test results will prompt you to ignore several tests, rather than fail. Once you have completed the corresponding function, you only need to delete the @ ignore annotation to perform a normal test.

  VII. fixture (temporarily translated as "fixed code segment ")

Fixture refers to the code that must be called at certain stages ". For example, in the above test, because only one calculator object is declared, its initial value is 0, but after the addition operation is tested, its value is not 0. Next, we test the subtraction operation, the result of the last addition operation must be considered. This is definitely a bad design! We strongly hope that each test is independent and there is no coupling between them. Therefore, it is necessary to perform a "Restore" Operation on the calculator object before each test is executed to eliminate the impact of other tests. Therefore, "the code that must be executed before any test is executed" is a fixture. We use @ before to mark it, as shown in the preceding example:

@Before
public void setUp() throws Exception ...{
calculator.clear();
}

The @ test annotation is not required here, because it is not a test, but a fixture. Similarly, if "the final work to be done after any test execution" is also a fixture, use @ after to mark it. Because this example is relatively simple, this function is not used.

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.