Directory
[TOC]
First, the JUnit concept
JUnit is the Java Language Unit test framework, in Java programs, a unit of work usually refers to a method, then unit testing is a method to test, the programmer generally through JUnit to complete a functional test of their own code, so unit testing can also be called programmer testing, If the tester is familiar with the method logic and its implementation, it is also possible to write JUnit unit tests for interface testing, i.e. white-box testing.
Second, JUnit test environment to build 2.1 point concept
more popular servers on the market: Tomcat (War),jboss (ear),WebLogic
more project building tools to use: Ant,maven: You can manage your project's dependencies online
2.2 How to import a jar package to build a JUnit test environment
Note: maven Central Warehouse URL
- The dependency packages required to create a common MAVEN project and their corresponding
- Junit-4.11.jar
- hamcrest-core-1.3
- The creation of a MAVEN project (JAR file) can be completed after adding the project's dependencies
2.3 How to create a MAVEN project to build a JUnit test environment
- Create a MAVEN project
Add a junit dependency in Pom.xml
2.3.1 Concrete Steps
- Create a MAVEN project
Modify Pom.xml
"' XML
```
Iii. Creating a test Case 3.1 considerations
- Create a new package under src/test/java for writing test cases
- All written test classes inherit from junit.framework.TestCase
- All methods in the test class need to start with test
"' Java
Package com.junit.exam01;
Import Junit.framework.TestCase;
All written test classes inherit from Junit.framework.TestCase
public class Example01 extends testcase{
All methods in the test class need to start with test
public void TestExam01 () {
System.out.println ("This was the first case");
}
public void testExam02() { System.out.println("This is the second case");}
}
4. 可以使用**注解**的方式(装饰器)来标注test方法(**@Test**,此时测试方法**不必以test开头**,也不必继承**TestCase**
Java
Package com.junit.exam01;
Import Org.junit.Test;
public class decoexam{
To annotate the test method by using annotations at this point the testing method does not have to be preceded by test
@Test
public void Decoexama () {
System.out.println ("This is Test A");
}
@Testpublic void DecoExamB() { System.out.println("This is test B");}
}
```
Iv. comments 4.1 @Test4.2 @Before
Executes once before all test methods are executed
Package com.junit.exam01;import Org.junit.Before;import org.junit.Test;import junit.framework.TestCase;///Note that before annotations in an inherited way doesn't work. Public classdecoexambefore{@Before Public void Funcbefore() {System. out.println("This is before"); }@Test Public void Decoexama() {System. out.println("This is Test A"); }@Test Public void Decoexamb() {System. out.println("This is Test B"); }}
4.3 @After
Executes once after all test methods have been executed
Package com.junit.exam01;import Org.junit.After;import org.junit.Test; Public classDecoexamafter {@After Public void Funcbefore() {System. out.println(" this was after"); }@Test Public void Decoexama() {System. out.println("This is Test A"); }@Test Public void Decoexamb() {System. out.println("This is Test B"); }}
4.4 @BeforeClass and @afterclass
The method of Beforeclass annotation is first executed once when the class is loaded.
Package com.junit.exam01;import Org.junit.AfterClass;import Org.junit.BeforeClass;import org.junit.Test; Public classDecoexamclassbefore2after {Private intresult =0;//Use the Static keyword for beforeclass and afterclass annotations @BeforeClass Public Static void Beforeexam() {System. out.println("This is class before"); }@AfterClass Public Static void Adterexam() {System. out.println(" this was class after"); }@Test Public void Exama() {System. out.println("This is Exama"); }@Test Public void Examb() {System. out.println("This is Examb"); }}
V. Assertion 5.1 Basic INTRODUCTION
- Assertequals (A, B)
Determine whether the two data is consistent, if the same test pass, otherwise indicates that the test does not pass, throw an assertion error
- Assertnotequals (A, B)
Determine if the two data is inconsistent, if the inconsistency means the test passed, otherwise it means the test does not pass, throw an assertion error
- Assertnull (...)
Determines whether a data is empty, if it is empty, the test passes, otherwise throws an assertion error
- Assertnotnull (...)
Determines whether a data is empty, if not NULL, the test passes, otherwise throws an assertion error
- Assertfalse (...)
Determines whether a data is true, if False, indicates that the test passed, otherwise throws an assertion error
- Asserttrue (...)
Determines whether a data is true, if true, indicates that the test passed, otherwise throws an assertion error
5.2 Cases
Package com.junit.exam01;import static org.junit.Assert.assertEquals;import Org.junit.Before;import org.junit.Test;import Com.junit.calc.CalcExam;import Junit.framework.Assert; Public classDemotestcalc {PrivateCalcexam Calc;@Before Public void Initcalc() {calc =New Calcexam(); }@Test Public void Add() {Calc.Add(3);intresult = Calc.GetResult(); System. out.println("The actual result is:"+ result); Assert.assertequals(3, result); }@Test Public void Sub() {Calc.Sub(3);intresult = Calc.GetResult(); System. out.println("The actual result is:"+ result); Assert.assertequals(-3, result); }@Test Public void Multi() {Calc.Multi(3);intresult = Calc.GetResult(); System. out.println("The actual result is:"+ result); Assert.assertequals(0, result); }@Test Public void Div() {Calc.Div(3);intresult = Calc.GetResult(); System. out.println("The actual result is:"+ result); Assert.assertequals(0, result); } }//***************************************************************************************************////The following is the contents of the Clac classPackage Com.junit.calc; Public classCalcexam {Private intresult =0; Public void Add(intA) {result + = A; } Public void Sub(intA) {result-= A; } Public void Div(intA) {if(A = =0)return; Result/= A; } Public void Multi(intA) {result *= A; } Public int GetResult() {return This.result; }}//----------------------------------------------------------------------------------------------------//
Vi. junit Packaging (test set implementation)
The ability to implement a test set requires a special runner, so we need to pass a parameter Suite.class (that is, * * @RunWith (suite.class) * *) to the @runwith callout. At the same time, we need another callout @suiteclasses to indicate that this class is a packaged test class with a basic syntax of * * @SuiteClasses ({..., ...}) * *, where the parameter is the class that needs to be tested
With the above two annotations, all the meanings have been fully expressed, so the following classes are irrelevant and the contents are empty.
package com.junit.exam01;import org.junit.runner.RunWith;import org.junit.runners.Suite;import org.junit.runners.Suite.SuiteClasses;// 固定写法,传递参数Suite.class@RunWith(Suite.class)// 将要测试的类写到里面@SuiteClasses({ // 由于用到的测试类都在同一个包中也不需要额外打包 DemoTestCalc.class, DecoExamAfter.class, DecoExamBefore.class, DecoExamTest.class, DecoExamClassBefore2After.class})// 这个类为空即可,但是必须保留publicclass DemoTestSuite {}
JAVA02 JUnit creation and simple implementation