JUnit 4.0 Experience

Source: Internet
Author: User
Tags reflection advantage

JUnit is the de facto standard unit test library for the Java language. JUnit 4 is the most landmark release of the library in three years. Its new feature simplifies testing by using tags in Java 5 (annotation) instead of using subclasses, reflection, or naming mechanisms to identify tests. In this article, the persistent Code tester, Elliotte Harold, takes JUnit 4 as an example detailing how to use this new framework in your own work. Note that this article assumes that the reader has experience with JUnit.

JUnit, developed by Kent Beck and Erich Gamma, is almost certainly the most important Third-party Java library that has been developed to date. As Martin Fowler said, "In the field of software development, there has never been so little code that has played such an important role." JUnit guides and facilitates the prevalence of testing. Because Junit,java code becomes more robust and more reliable, bugs are less than ever. JUnit (which is inspired by Smalltalk's Sunit) derives many xUnit tools that apply the advantages of unit testing to various languages. NUnit (. NET), Pyunit (Python), CppUnit (c + +), Dunit (Delphi), and other tools affect the testing of programmers on a variety of platforms and languages.

However, JUnit is just a tool. The real advantage comes from the ideas and techniques that JUnit uses, not the framework itself. Unit testing, test-first programming, and test-driven development are not all implemented in JUnit, and any programming that compares GUI must be done with Swing. The last update of JUnit itself was almost three years ago. Although it has proven to be more robust and durable than most frameworks, bugs are found, and more importantly, Java is evolving. The Java language now supports generics, enumerations, variable-length parameter lists, and annotations, which bring new possibilities for reusable framework design.

JUnit's stagnation has not been defeated by programmers who want to scrap it. Challenger includes Bill Venners's Artima Suiterunner and Beust of Cedric TestNG. These libraries have some notable features, but they are not up to JUnit's visibility and market share. None of them has extensive out-of-the-box support in products such as Ant, Maven, or Eclipse. So Beck and Gamma started developing a new version of JUnit that took advantage of the new features of Java 5, especially the annotations, making unit testing simpler than using the original junit. In Beck's words, "the theme of JUnit 4 is to encourage more developers to write more tests by simplifying junit further." "JUnit 4, while maintaining backward compatibility with the existing JUnit 3.8 test suite, is still committed to the most significant improvement in Java unit testing since JUnit 1.0."

Note: The improvement of the framework is quite cutting-edge. Although the big outlines of JUnit 4 are clear, the details can still be changed. This means that this article is a preemptive look at JUnit 4, not its final effect.

Test method

All previous versions of JUnit used naming conventions and reflection to locate tests. For example, the following code test 1+1 equals 2:

import junit.framework.TestCase;
public class AdditionTest extends TestCase {
  private int x = 1;
  private int y = 1;
  public void testAddition() {
   int z = x + y; assertEquals(2, z);
  }
}

In JUnit 4, tests are identified by @Test annotations, as follows:

import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
  private int x = 1;
  private int y = 1;
  @Test public void testAddition() {
   int z = x + y;
   assertEquals(2, z);
  }
}

The advantage of using annotations is that you no longer need to name all the methods Testfoo (), Testbar (), and so on. For example, the following methods can also work:

import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
  private int x = 1;
  private int y = 1;
  @Test public void additionTest() {
   int z = x + y; assertEquals(2, z);
  }
}

The following method can also work:

import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
  private int x = 1;
  private int y = 1;
  @Test public void addition() {
   int z = x + y; assertEquals(2, z);
  }
}

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.