Company development programmers have a lot of beautiful women, and then we are not to be outdone. The study of JUnit, belonging to just contact, did a bit of note-sharing. Only suitable small white articles, have been the entry of the negligible
First, the required tools:
1. There are eclipse 4.3 64-bit (or other Java development tools), download path: Http://pan.baidu.com/s/1qYLVumo;
2. Install the JDK, I installed the 1.8 version, the configuration of the environment (network not to share, to download their own website);
3. jar package with junit4.12, for: Http://pan.baidu.com/s/1i548ACP;
Ii. Creation of the project
My project package structure is as follows:
1. Open Eclipse and create a new Java project package;
2. Import the JUnit package;
Select Java Project Package-right--->properties-->java Bulid path-->libraries-->add External jars...-->junit4.12 within the 2 jar package---OK
is the imported successful picture:
3. Create a new package, and class;
The code for the class is as follows:
1 PackageTestjunit.liang;2 3 Public classUser {4 5 //Define Age6 Public intAgeintAge ) {7 8 returnAge ;9 Ten } One //Define name A Publicstring Name (string name) { - - returnname; the - } - //Define gender - Publicstring Sex (String sex) { + - returnsex; + A } at}
3. Create a new package, class, tested and validated with JUnit;
The code is as follows:
1 Packagetestjunit.test;2 3 ImportOrg.junit.Assert;4 Importorg.junit.Test;5 6 ImportTestJunit.liang.User;7 8 Public classTestuserjunit {9 TenUser User =NewUser (); One A @Test - Public voidTestage () { - //Assign Value the intAge = User.age (18); - //adding assertions -Assert.assertequals (age, 18); - } + @Test - Public voidtestname () { +String name = User.Name ("Light Rain"); AAssert.assertequals (name, "Light Rain"); at } - @Test - Public voidTestsex () { -String sex = user.sex ("female"); -Assert.assertequals (Sex, "female"); - } in - to}
In the back of this class, check-right-click and select runs As-->junit Test
The results are as follows:
Third, Code Analysis
1.Junit package After the successful import, write the test class, before each method to add @test, otherwise, the test will not run the method.
If the following 2 methods are annotated, only one method will be run when running.
White box Test (Java)-(JUnit introductory article i)