For the code to be tested, see JUnit 4.0. Complete the code for the last Division test.
Package junitexample; public class dividecalculate implements calculate {@ overridepublic int invoke (int... i) {// todo auto-generated method stubint _ I = I [0]; for (int m = 1; m <I. length; m ++) {if (I [m] = 0) {Throw new arithmeticexception ("Division cannot be zero ");}
// If you do not judge before, an exception will also be thrown here, so that you can see the exception thrown by the above Code during the test _ I/= I [m];} return _ I ;}}
The following is the junit3.8 test code:
1. Division Test
Package junitexample; import JUnit. framework. testcase;/** JUnit 3 **/public class calculatetest0 extends testcase {public void testdivide1 () {calculate c = new dividecalculate (); int result = C. invoke (10, 2); assertequals (5, result);} public void testdivide2 () {throwable E = NULL; Calculate c = new dividecalculate (); try {C. invoke (10, 0); fail ();} catch (exception ER) {e = ER;} assertnotnull (E); assertequals (arithmeticexception. class, E. getclass (); assertequals ("the divisor cannot be zero", E. getmessage ());}}
2. Multiplication test:
package JunitExample;import junit.framework.TestCase;import JunitExample.AddCalculate;import JunitExample.Calculate;import JunitExample.MultiplyCalculate;/** junit 3 **/public class CalculateTest1 extends TestCase {public void testAdd(){Calculate c=new AddCalculate();int result=c.invoke(2,3,4,5,2,-1,-5);assertEquals(10,result);}public void testMultiply(){Calculate c=new MultiplyCalculate();int result=c.invoke(2,3,4,5,2,-1,-5);assertEquals(1200,result);}}
3. Run the two tests in one class.
package JunitExample;import junit.framework.TestCase;public class TestAll extends TestCase {private void Suite() {// TODO Auto-generated method stub}}