Unit testing with JUNIT4 in Eclipse (intermediate)

Source: Internet
Author: User
Tags assert

This article transferred from: http://blog.csdn.net/andycpp/article/details/1327346

We continue to analyze the examples in the primary article. In the introductory chapter we use Eclipse to automatically generate a test framework, in this article, we will carefully analyze the test framework of each of the details, know it more to know why, in order to become more proficient in the application of JUNIT4.

A. Contains the necessary package

The JUNIT4 framework is used in the test class, and the package is naturally included in the corresponding package. One of the most important package is org.junit.*. After it is included, most of the features are there. There is also a word that is very important "import static org.junit.assert.*;", we use a series of assertequals methods when testing from this package. Note that this is a static inclusion (static), which is a new feature added in JDK5. In other words, Assertequals is a series of static methods in an Assert class, and is generally used in an assert manner. Assertequals (), but with static inclusion, the preceding class name can be omitted and used more conveniently.

II. Declaration of the Test class

It is noted that our test class is a separate class, without any parent class. The name of the test class can also be arbitrarily named, without any limitations. So we cannot tell by the declaration of a class that it is not a test class, but that it differs from the ordinary class in its declaration of methods inside it, which we will then talk about.

Third, create an object to be tested.

Which class you want to test, you'll first create an object of that class. As the code in the previous article:

private static Calculator Calculator = new Calculator ();

In order to test the calculator class, we must create a calculator object.

Iv. Declaration of test methods

In a test class, not every method is used for testing, and you must use "callout" to clearly indicate which is the test method. "Labeling" is also a new feature of JDK5, which is very appropriate here. As we can see, there are @before, @Test, @Ignore and so on before some methods, and these are the annotations, which begin with an "@". These annotations are JUNIT4 customized and it is important to master the meaning of these annotations.

Five, write a simple test method.

First, you will use the @test callout in front of the method to indicate that this is a test method. The declaration of a method has the following requirements: The name can be arbitrarily taken without any restrictions, but the return value must be void and cannot have any arguments. If these rules are violated, an exception is thrown at run time. As for what to write in the method, it depends on what you need to test. Like what:

@Test

public void Testadd () {

Calculator.add (2);

Calculator.add (3);

Assertequals (5, Calculator.getresult ());

}

We want to test the "addition" function when the correct, in the test method called several times the Add function, the initial value of 0, plus 2, plus 3, we expect the result should be 5. If the final actual result is also 5, then the Add method is correct, and the reverse means it is wrong. Assertequals (5, Calculator.getresult ()); Is to determine whether the expected results and actual results are equal, the first parameter fills in the expected result, the second parameter fills in the actual result, that is, the results obtained by the calculation. When this is done, JUnit automatically tests and feeds the test results back to the user.

VI. Ignore testing some of the unfinished methods.

If you do a good job planning before you write the program, then what is the function should be fixed. Therefore, even if the method is not yet complete, his specific functionality is deterministic, which means that you can write test cases for him. However, if you have finished writing the test case for the method, but the method is not yet complete, then the test must be "failed". This failure is different from real failure, so JUnit provides a way to differentiate them by adding a @ignore callout to the test function, meaning that "some methods have not been completed and are not currently participating in the test." In this case, the test results will prompt you to have several tests ignored, not failures. Once you have completed the corresponding function, just delete the @ignore annotation and you can perform the normal test.

Vii. Fixture(translated as "fixed code snippet")

The meaning of fixture is "code that must be called at certain stages". For example, the above test, because only a calculator object is declared, his initial value is 0, but after testing the addition operation, his value is not 0, and then test the subtraction operation, it is necessary to consider the results of the last addition operation. This is definitely a very bad design! We very much hope that each test is independent and that there is no coupling between each other. Therefore, it is necessary for us to perform a "undo" operation on the Calculator object before executing each test to eliminate the impact of other tests. Therefore, "code that must be executed before any test execution" is a fixture, and we use @before to label it as shown in the previous example:

@Before

public void SetUp () throws Exception {

Calculator.clear ();

}

There is no need for @test labeling here, because this is not a test, but a fixture. Similarly, if the "finishing touches required after any test execution" is also a fixture, use @after to annotate. Because this example is relatively simple, this function is not used.

Some of the basics of JUNIT4 are introduced here, and there are some more flexible uses in this series of high-level articles to introduce you!

Unit testing with JUNIT4 in Eclipse (intermediate)

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.