Getting started with JUnit

xiaoxiao2021-03-06  39

First, introduction

JUnit is an open source Java unit test framework. In 1997, Erich Gamma and Kent Beck were developed. These two cows in Erich Gamma is one of GOF; Kent Beck is important in XP (you think that you are not surprising at all).

As usual: "The sparrow is small, the five organs are complete." JUnit is very small, but the function is very powerful.

Below is a summary of some characteristics of JUnit:

1) The API provided allows you to write the test results of the test results.

2) Provide three ways to display your test results, but also expand

3) Provide a function of unit testing

4) Ultra-lightweight and use simple, no commercial deception and useless wizard

5) The whole frame design is good, easy to expand

The measured objects of different properties, such as Class, JSP, Servlet, EJB, etc., JUnit has different skills. Due to the nature of this article, the following only Class test is taken as an example.

Let's open the door of JUnit!

Second, download

Click http://www.junit.org to download the latest version of JUnit, this article is used for

3.8.1

Version. As for the installation or configuration, you only need to easily download the JAR file in the compressed package, and you can put it in the classpath you engineered.

This way, you can use JUnit to write unit test code in your system (it's very simple)!

Third, HelloWorld

I can remember that I can find the introductory code of HelloWorld in almost every language teaching book. Here are here today, we also start with an example of simply not worthy of unit testing. This is a super simple calculator that will only do two numbers and subtraction (the first grade of primary school). code show as below:

Public Class SampleCalculator

{

Public Int Add (int augend, int address)

{

Return Augend Addend;

}

Public int subtration (int smund, int subtrahend)

{

Return minuend - Subtrahend;

}

}

Compile the above code. Below is a unit test case for I write for the above program:

/ / Please note the characteristics of the class name and method name in this program

Public Class Testsample Extends Testcase

{

Public void testAdd ()

{

SampleCalculator Calculator = New SampleCalculator ();

Int results = Calculator.Add (50, 20);

askERTEQUALS (70, Result);

}

Public void testsubtration ()

{

SampleCalculator Calculator = New SampleCalculator ();

Int result = Calculator.Subtration (50, 20);

askERTEQUALS (30, result);

}

}

Ok, enter Javac-ClassPath in the DOS command line .; junit.jar testsample.java compiles the test class. Then enter java -classpath .; junit.jar junit.swingui.teStrunner TestSample Run the test class, you will see the following window.

In the above picture, the green description unit test passes, there is no error generation; if it is red, it is said that the test failed. Such a simple unit test is complete, is it easy? According to the framework: All test classes must be inherited from the JUnit.Framework.TestCase class; inside the test method, the naming should start with Test, must be public void and not have parameters; Methods A functional single method is tested; use Assertequals and other junit.framework.testcase to determine whether the test results are correct or not.

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

Four, forward

After learning HelloWorld, you can write a standard unit test case. But there are some details, here you have to explain it. Don't worry, very fast!

When you look at the code above, is it noticed that there is a SampleCalculator initialization statement in each Testxxx method? This is obviously not in line with the coding specification. You may be to extract it out to put it in the constructor. And slow! Initialization in JUnit is suggested in the setup method. JUnit provides a pair method, one for initializing some necessary conditions before running test methods, and the other is the condition that the initialization is removed after the test is completed (see the figure below).

In addition, do you noticed that a detail of the window is popped up, under the Green Border has Errors, Failures Statistics. What is the difference between these?

Failures as a mistake of the unit test, which indicates that your code has bugs, but it may be that your unit test code has a logical error (note is a logic error). Errors is not what you expect, and an error can be checked in the order below:

Check the environment you need to test, such as: Database connection

Check unit test code

Check your system code

Five, batch running TEST CASE

This is one of the JUnit features mentioned earlier. It is convenient for the batch operation of the system unit test. It is also very simple 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, run, and the way to TestSample above.

JAVAC-ClassPath .; junit.jar Testall.java

Junit.jar junit.swingui.teStrunner Testall

how about it? This way you add a few TestCase in the Suite method, and it can also add TestSuite to add a small collection of collections to the big collection, which is convenient for continuously increased TestCase management and maintenance.

Oh, do you think the role of the suite method is not the main MAIN of a Java application? And the Suite here must strictly abide by the above way!

Sixth, Testrunner

Testrunner represented in three ways has been given in JUnit. You can run your experience separately.

Junit.swingui.teStrunner

JUnit.awtui.teStrunner

Junit.textui.teStrunner

Seven, summary

This article easily introduces the entry knowledge of JUnit use. There is no in-depth technique and normal use specification. And these please pay attention to the article about JUnit Advanced Use, JUnit Source Code Analysis.

转载请注明原文地址:https://www.9cbs.com/read-53355.html

New Post(0)