Junit 3.8 notebooks

Source: Internet
Author: User

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}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.