Pursue code Quality

Source: Internet
Author: User
Tags assert testng

After a long period of active development, JUnit 4.0 was released early this year. Some of the most interesting changes to the JUNIT framework-especially for readers of this column-are implemented by using annotations cleverly. In addition to significant improvements in appearance and style, the new framework features the design of test cases freed from structural rules. The original rigid fixture model is more flexible, which is advantageous to adopt a more configurable method. Therefore, the JUNIT framework no longer requires that each test work be defined as a method that begins with test, and that you can now run fixture only once, rather than running it once per test.

While these changes are comforting, JUnit 4 is not the first Java™ test framework to provide a flexible model based on annotations. Long before modifying JUnit, TestNG was established as a framework based on annotations.

In fact, TestNG was the first in Java programming to use annotations for testing, making it a strong competitor for JUnit. However, since JUnit 4 was released, many developers have questioned: is there any difference between the two? In this month's column, I'll discuss some of the features of TestNG different from JUnit 4, and propose ways to make the two frameworks continue to complement each other rather than compete with each other.

Did you know that?

Running JUnit 4 tests in Ant is much more difficult than expected. In fact, some teams have found that the only solution is to upgrade to Ant 1.7.

The resemblance on the surface

JUnit 4 and TestNG have some common and important features. Both of these frameworks make the test work surprisingly simple (and enjoyable), facilitating the testing effort. Both also have active communities that provide support for active development while generating rich documentation.

The difference between the two frameworks is the core design. JUnit has always been a unit-testing framework, that is, its purpose is to facilitate testing of individual objects, and it does perform such tasks extremely efficiently. TestNG is used to solve a higher level of test problems, so it has some features that are not in JUnit.

A simple test case

At first glance, the tests implemented in JUnit 4 and TestNG are very similar. To better understand what I mean, take a look at the code in Listing 1. This is a JUnit 4 test that has a macro-fixture (that is, fixture that is called only once before all tests are run), and this macro-fixture is represented by the @BeforeClass attribute:

Listing 1. A simple JUnit 4 test case

package TEST.COM.ACME.DONA.DEP;

Import static org.junit.Assert.assertEquals;
Import static org.junit.Assert.assertNotNull;
Import Org.junit.BeforeClass;
Import org.junit.Test;

public class Dependencyfindertest {
private static Dependencyfinder finder;
 
@BeforeClass
public static void Init () throws Exception {
Finder = new Dependencyfinder ();
}

@Test
public void verifydependencies ()
throws Exception {
String targetclss =
tes T.com.acme.dona.dep.dependencyfind ";

filter[] filtr = new filter[] {
New Regexpackagefilter ("java|junit|org")};

dependency[] Deps =
Finder.finddependencies (TARGETCLSS, filtr);

Assertnotnull ("Deps was null", deps);
Assertequals ("should be 5 large", 5, Deps.length);
}
}

The JUnit user immediately notices that there are no grammatical elements required in the previous version of JUnit in this class. This class does not have a setUp () method, nor does it extend to the TestCase class, nor does it even have a method whose name begins with Test. This class also leverages some of the features of Java 5, such as static imports, and it is obvious that it also uses annotations.

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.