Getting started with JUnit

Source: Internet
Author: User

I. Introduction

JUnit is an open-source Java unit testing framework. Developed by Erich Gamma and Kent Beck in 1997. Erich Gamma is one of the two cattle, and Kent Beck has made significant contributions to XP (you don't think it's strange to be familiar with it ).

As the saying goes: "The Sparrow is small and dirty ." JUnit is designed to be very small but powerful.

The following is a summary of some features of JUnit:

1) The provided API allows you to write reusable unit test cases with clear test results

2) three methods are provided to display your test results, and you can expand

3) provides the unit test case batch operation function

4) ultra-lightweight and easy to use, without commercial spoofing and useless wizard

5) the entire framework is well designed and easy to expand

JUnit has different usage skills for tested objects of different nature, such as class, JSP, Servlet, and EJB. Due to the nature of this article, the following uses the class test as an example.

Now let's open the door to JUnit!

Ii. Download

Click the http://www.junit.org to download to the latest JUnit version, which is 3.8.1. As for installation or configuration, you only need to easily put the JAR file in the downloaded package into your project's classpath.

In this way, you can use JUnit to write unit test code in your system (isn't it easy )!

Iii. helloworld

Remember to find the Getting Started code helloworld in almost every language teaching book. Today, we also start with an example that is simple enough to avoid unit tests. This is a super simple calculator that can only add or subtract two numbers (the first-year elementary school must be the best ). The Code is as follows:

Public class samplecalculator

{

Public int add (INT augend, int addend)

{

Return augend + addend;

}

Public int subtration (INT minuend, int subtrahend)

{

Return minuend-subtrahend;

}

}

Compile the above Code. Below is a unit test case I wrote for the above program:

// Note the features of the Class Name and method name in this program.

Public class testsample extends testcase

{

Public void testadd ()

{

Samplecalculator calculator = new samplecalculator ();

Int result = calculator. Add (50, 20 );

Assertequals (70, result );

}

Public void testsubtration ()

{

Samplecalculator calculator = new samplecalculator ();

Int result = calculator. subtration (50, 20 );

Assertequals (30, result );

}

}

All right, enter javac-classpath.; JUnit. Jar testsample. Java in the doscommand line to compile the test class. Enter Java-classpath.; JUnit. Jar JUnit. swingui. testrunner testsample to run the test class. You will see the following window.

Green indicates that the unit test is passed and no errors are generated. If it is red, the test fails. This simple unit test is complete, isn't it easy?

According to the Framework: All test classes written must inherit from JUnit. framework. the test method in the testcase class, which must start with test and must be public void and cannot have parameters, try to use a single testxxx method to test a single function method, and use JUnit such as assertequals. framework. the asserted method in testcase is used to determine whether the test result is correct or not.

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

4. Step forward

After learning helloworld, you can write standard unit test cases. But there are some details, which should be explained here. Don't worry. Soon!

When you look at the above Code, do you notice that each testxxx method has a samplecalculator initialization statement? This obviously does not conform to the encoding specification. You may be about to extract it and put it in the constructor. Slow! Initialization in JUnit is recommended in the setup method. JUnit provides a pair of methods. One is to initialize some essential conditions before running the test method, and the other is to remove the initialization conditions after the test is completed (see ).

In addition, do you notice the details in the pop-up window above, which include errors and failures statistics under the green bar. What is the difference between the two?

Failures is the expected error in unit testing. It indicates that your code has bugs, but it may also be that your unit test code has logical errors (note that it is a logical error ). Errors is not what you expected. If an error occurs, you can check it in the following order:

Check the environment required for the test, such as database connection.

Check unit test code

Check your system code

5. Run test case in batches

This is one of the JUnit features mentioned above. It facilitates the batch operation of system unit tests. It is also very easy to use. first look at the 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;

}

}

This test program is compiled and run in the same way as the testsample method above.

Javac-classpath.; JUnit. Jar testall. Java

Java-classpath.; JUnit. Jar JUnit. swingui. testrunner testall

How is it? In this way, you can add several testcase files to the suite method to run several files, and you can also add testsuite to add a smaller set to a larger set, it facilitates the management and maintenance of the increasing testcase.

Well, do you think the role of the suite method is similar to that of the main program of Java applications? And the suite here must strictly abide by the above writing!

Vi. testrunner

In JUnit, three methods of testrunner are provided. You can run them separately to experience their differences.

JUnit. swingui. testrunner

JUnit. awtui. testrunner

JUnit. textui. testrunner

VII. Summary

This article briefly introduces how to use JUnit. There is no in-depth technique or usage specification involved. Please pay attention to my articles on advanced use of JUnit and source code analysis of JUnit.

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.