11th Unit Test Tool JUnit Learning

Source: Internet
Author: User

1, write a simple arithmetic program

UnitTest class file:

public class UnitTest {     int A;     int b;     int answer;//correct answer public          int plus (int a,int b) {      answer = a + b;     return answer; } public          int minus (int a,int b) {     answer =-A;     return answer; } public          int multiply (int a,int b) {     answer = a * b;     return answer; } public          int divide (int a,int b) {     answer = A/b;     return answer; } public     int GetResult () {return answer;}}

Expression class file:

Import java.util.*;p Ublic class Expression {     int A;     int b;     static int c;//user answer     int answer;//correct answer     static Scanner in=new Scanner (system.in);      Public Expression () {     a = new Random (). Nextint ()%10;     b = new Random (). Nextint ()%10;     System.out.print ("+a+" + "+b+" = ");  }         public static void Main (string[] args) {int answer; Expression expression = new expression (); UnitTest u = new UnitTest () answer = U.plus (Expression.a, expression.b); try{expression.c = In.nextint ();} catch (Inputmismatchexception e) {   System.err.println ("\nerror  , please Enter a Int number");} if (answer==c) {System.out.print ("correct");} Else{system.out.println ("wrong answer"); System.out.println ("The answer is" +answer);}}
}

2. Adding JUNIT4 controls

Right-click Project file, select Properties option

Select left column Java Bulid Path, click on the Library tab, then click on the Add Library on the right ...

Click on JUnit and choose version JUnit4

, JUnit library files have been introduced

3. Write Test class

After that, right-click Unittest.java-->new-->junit Test case

Select the method you want to test:

The myeclipse will then automatically generate a Unittesttest class that will be tested by changing the fail method in the generated class to its own code.

The code is as follows:

Unittesttest class file

Import static Org.junit.assert.*;import Org.junit.test;public class Unittesttest {public static unittest Puls = new Unitte St ();p ublic static int answer; @Testpublic void Testplus () {puls.plus (); Assertequals (2, Puls.getresult ());} @Testpublic void Testminus () {puls.minus; Assertequals (0, Puls.getresult ());} @Testpublic void Testmultiply () {puls.multiply; Assertequals (1, Puls.getresult ());} @Test (expected=arithmeticexception.class) public void TestDivide1 () {    puls.divide (1, 0);} @Testpublic void Testdivide () {puls.divide; Assertequals (1, Puls.getresult ());}}

4, run the test:

Right-click Junittesttest,run as-->junit Test

The test results show that the code is correct, there are no bugs, and no changes are required.

The actual operation of this article learning from the online, now provides the original learning document URL for everyone to learn from: Http://www.tuicool.com/articles/fArMFjJ

11th Unit Test Tool JUnit Learning

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.