1. What is JUnit?
JUnit (Java unit) is a Java unit testing framework used for testing methods.
2. How to Use JUnit? I. @ test method in the test class (the JUnit. jar package is required)
Jar: http://download.csdn.net/detail/u010653050/5744487
Person. Java
Package demo; // The method for running the public void run () {system. out. println ("Run");} // method to eat public void eat () {system. out. println ("eat ");}}
Junittest. Java
package demo;import org.junit.Test;public class JunitTest {@Testpublic void TestRun() {Person person = new Person();person.run();}@Testpublic void TestEat() {Person person = new Person();person.eat();}}
This is the most basic method test. First, import the required package, open the myeclipse outline, and right-click the method in the outline, if the prompt box of a bar in the view JUnit after running is green, the test passes. If it is red, the test fails.
If there is output content, it will be output in the console.
2. @ before @ after
Junitafterbeforetest. Java
package demo;import org.junit.After;import org.junit.Before;import org.junit.Test;public class JunitAfterBeforeTest {@Beforepublic void before() {System.out.println("before");}@Testpublic void TestRun() {Person person = new Person();person.run();}@Testpublic void TestEat() {Person person = new Person();person.eat();}@Afterpublic void after() {System.out.println("after");}}
3. @ before @ after usage-Initialize resources and destroy resources
Package demo; import Org. JUnit. after; import Org. JUnit. before; import Org. JUnit. test; public class junitbeforeaftertest2 {person = NULL; @ beforepublic void before () {// initialize the person object to reduce the amount of code: person = new person ();} @ testpublic void testrun () {person. run () ;}@ testpublic void testeat () {person. eat () ;}@ afterpublic void after () {system. out. println ("after ");}}4. @ beforeclass @ afterclass
Junitbeforclassafterclasstest. Java
package demo;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;public class JunitBeforclassAfterClassTest {@BeforeClasspublic static void beforeClass() {System.out.println("beforeClass");}@Testpublic void TestRun() {Person person = new Person();person.run();}@Testpublic void TestEat() {Person person = new Person();person.eat();}@AfterClasspublic static void afterClass(){System.out.println("afterClass");}}
5. Differences between @ before @ After and @ beforeclass @ afterclass
@ Beforeclass @ afterclass is run before the class is loaded.
Vi. Assert)
Junitasserttest. Java
Package demo; import Org. JUnit. assert; import Org. JUnit. test; public class junitasserttest {@ testpublic void testrun () {// asserted // determine whether the value returned by the method is the same. If the value is the same, the test passes. Otherwise, the test does not pass assert. assertequals ("1", "1") ;}@ testpublic void testassertarrayequals () {// checks whether the returned array is the same. The same test passes, but not the same test does not pass assert. assertarrayequals (New int [] {1, 1}, new int [] {1, 1}) ;}@ testpublic void testasserttrue () {// determine whether the value returned by the method is true or false. If the value is true, the request fails. If the value is false, the request fails. // assert. asserttrue (true); assert. asserttrue (false) ;}@ testpublic void testassertfalse () {// determine whether the value returned by the method is true, false, false, true, not assert. assertfalse (false); assert. assertfalse (true );}}