Demo.java
Package JUnit;/** * Java Build Path--add Library Junit 4 * @author kj **/ Public classDemo { Public intAddintAintb) { Try{Thread.Sleep ( -); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnA +b; } Public intDevide (intAintb) { returnA/b; }}
Demotest.java
Package Test;import JUnit. Demo;import Org.junit.assert;import org.junit.Test;/** * Method of test: * 1. Methods must be common; * 2. The method return value must be void * 3. The method must be non-parametric * 4. Must be annotated with @test * 5. @before: Methods that must be called once before each test method executes * @beforeclass: is a method static that must be and can only be called once before all test methods are executed. * @after * @afterclass: * 6. * @author KJ **/ Public classdemotest {@Test (timeout= -)//Maximum execution time is 100ms long type is generally MS Unit Public voidTestadd () {Demo D=NewDemo (); Assert.assertequals (Ten, D.add (4,6)); } @Test (Expected=java.lang.arithmeticexception.class)//A mathematical anomaly occurred Public voidtestdevide () {Demo D=NewDemo (); Assert.assertequals (2, D.devide (Ten,0)); }}
JUnit Unit Test