Getting Started with JUnit

Source: Internet
Author: User

First, Introduction

JUnit is an open-source Java Unit Testing framework. In 1997, the development was completed by Erich Gamma and Kent Beck. Erich Gamma is one of the two GOF, and Kent Beck has an important contribution in XP (you don't seem to be surprised at all).

As the saying goes: "Not very much, perfectly formed." "JUnit is designed to be very small, but it is very powerful.

Here is a summary of some of the JUnit features:

1) provides an API that allows you to write a reusable unit test case with a clear test result

2) provides three ways to display your test results, and can also extend

3) provides functionality for unit test cases to run in batches

4) ultra-lightweight and easy to use, no commercial deception and useless wizards

5) The whole frame is well designed and easy to expand

JUnit has different techniques for using objects of different natures, such as CLASS,JSP,SERVLET,EJB. Due to the nature of this article, the following are only examples of class tests.

Let's open the gates of JUnit!

Second, download

Click http://www.junit.org to download to the latest version of JUnit, this article uses version 3.8.1. As for installation or configuration, you just need to easily download the jar files from the downloaded package and put them in the classpath of your project.

Third, the HelloWorld code is as follows:

 Public class samplecalculator{       publicint Add (intint  addend)       {               return augend + addend;       }         Public int subtration (intint  subtrahend)       {               return Minuend- subtrahend;       }}

Compile the above code to pass. Here is a unit test case I wrote for the above program:

Note the characteristics of the class name and method name in this program

 Public classTestsample extends testcase{ Public voidTestadd () {samplecalculator calculator=NewSamplecalculator (); intresult = Calculator.add ( -, -); Assertequals ( -, result); }        Public voidtestsubtration () {samplecalculator calculator=NewSamplecalculator (); intresult = Calculator.subtration ( -, -); Assertequals ( -, result); }}

Enter Javac-classpath in the DOS command line.: Junit.jar Testsample.java compiles the test class through. then enter JAVA-CP.: Junit-4.12.jar junit.textui.TestRunner testsample Run the test class and you will see the following window.

, green indicates that the unit test passed without error, and if it is red, then the test fails. So a simple unit test is done, isn't it easy?

According to the framework: all test classes written must inherit from the Junit.framework.TestCase class, the test method inside, the name should start with test, must be public void and cannot have parameters, and in order to test the error is convenient, Try to test a single-function method with a Testxxx method, and use the assertion methods in junit.framework.TestCase such as assertequals to determine whether the test results are correct or not.

You can compare the implementation of the above test class to understand the rules--very simple! And how many test methods you have in this test class, how many test methods you will run.

Four, one step forward

Having finished HelloWorld, you can already write standard unit test cases. But there are some details, and here's a little bit more to explain. Don't worry, it's fast!

When you look at the code above, do you notice that there is a Samplecalculator initialization statement in each Testxxx method? This is clearly incompatible with coding specifications. You're probably just going to pull it out and put it in the constructor. Wait! Initialization in JUnit is recommended in the Setup method. JUnit provides a pair of methods, one that initializes some prerequisites before running the test method, and the other is the condition to remove the initialization after the test is complete (see).


In addition, did you notice that a detail of the pop-up window above, there are errors, failures statistics under the green bar. What is the difference between the two?

Failures as an error expected by the unit test, it indicates that your code has a bug, but it is also possible that your unit test code has a logic error (note is a logic error). Errors is not what you expect, there is an error you can check in the following order:

Examine the environments required for testing, such as: Database connections

Check Unit test Code

Check your system code

Five, batch run test case

This is one of the JUnit features mentioned earlier. It facilitates batch operation of system unit tests. It is also very simple to use, first look at the use of code:

Import Junit.framework.Test;

Import Junit.framework.TestSuite;

public class testall{

public static Test Suite () {

TestSuite Suite = new TestSuite ("TestSuite Test");

Suite.addtestsuite (Testsample.class);

return suite;

}

}

The test program compiles, runs, and testsample the same way as above.

Javac-classpath.; Junit.jar Testall.java

Java-classpath.; Junit.jar Junit.swingui.TestRunner Testall

What do you think? This allows you to add a few testcase to the suite method and it can also add testsuite to incorporate smaller collections into large collections, facilitating the management and maintenance of an ever-increasing testcase.

Oh, do you think that the role of the suite method is similar to the Java application's main? And the suite here must strictly abide by the above-mentioned wording!

Liu, Testrunner

There are three ways to represent Testrunner in JUnit. You can run the experience separately under their different.

Junit.swingui.TestRunner

Junit.awtui.TestRunner

Junit.textui.TestRunner

Vii. Summary

This article provides a quick and easy introduction to junit usage. There is no deep skill or specification involved at all. Please follow my article on JUnit advanced usage, JUnit source code analysis.

Getting Started with JUnit

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.