JUnit 4 Unit Test

Source: Internet
Author: User

individual Project

      --junit 4 Unit Test

Learning to JUnit unit tests, I tested a calculator I wrote before (two dependent classes: Calc.java Calcfunction.java). The code has been placed in GitHub.

Post part of the code:

1  Public classCalcextendsjavax.swing.jframe{2 3          PublicCalc () {4 initcomponents ();5         }6 7         Private voidinitcomponents () {8 java.awt.GridBagConstraints gridbagconstraints;9 Ten setdefaultcloseoperation (Javax.swing.WindowConstants.EXIT_ON_CLOSE); OneSettitle ("\u8ba1\u7b97\u5668"); ASetBounds (NewJava.awt.Rectangle (500, 50, 0, 0)); -Setresizable (false); -  the···//Interface Layout -  -                 } -                 //Main monitoring +ActionListener listenerequals =NewActionListener () { -              Public voidactionperformed (ActionEvent e) { +Formulastand.settext (edit + "="); AHaspoint =false; at String result; -                 Try { -result = Calcfunction.evaluate (edit+ "="); -}Catch(Exception E1) { -result = "Syntax error"; -                 } in                 if(double.parsedouble (result)%1 = = 0) { -Editstand.settext (String.valueof (int) (double.parsedouble (Result) )); to}Else{ +Editstand.settext (String.valueof (float) (double.parsedouble (Result) )); -                 } theEdit = "0"; *             } $         }; Panax Notoginseng}
Calc.java

Classes used for testing

Calcfunction.java

Unit tests are below

The first thing to do is to import it under this project JUnit Library Specific practices: Build path--Config build path ..... Add Libraries ... Select JUnit next-> Select junit 4 finished

Add complete

Creating test Cases

New--Java test Case Name:calcfunctiontest Next-> Select the method to test

Automatically generate a test class calcfunctiontest, which contains some empty test cases, @Test annotations. You only need to make these test cases slightly modified to use. The complete Calcfunctiontest code is as follows:

1  Packagecom.school.indivodial;2 3 Import Staticorg.junit.assert.*;4 5 ImportOrg.junit.Before;6 Importorg.junit.Test;7 8 /**9  * Ten  * @authorLendoon One The *calcfunction class is a function class of the Calc class, A *calc UI input infix expression called Calcfunction static Evaluate method evaluates the returned result -  */ -  Public classCalcfunctiontest { the      - @Test -      Public voidTestevaluate_basic ()throwsException { -Double expectedanswer = double.valueof ("90"); +Double actualanswer = double.valueof (Calcfunction.evaluate ("100-(22+6x3) ÷4=")); - assertequals (Expectedanswer, actualanswer); +     } A  at @Test -      Public voidTestevaluate_pow ()throwsexception{ -Double expectedanswer = double.valueof ("1024"); -Double actualanswer = double.valueof (Calcfunction.evaluate ("2^10=")); - assertequals (Expectedanswer, actualanswer); -     } in      - @Test to      Public voidTESTEVALUATE_SQRT ()throwsexception{ +Double expectedanswer = double.valueof ("8"); -Double actualanswer = double.valueof (Calcfunction.evaluate ("√64=")); the assertequals (Expectedanswer, actualanswer); *     } $     Panax Notoginseng @Test -      Public voidTestevaluate_cos ()throwsexception{ theDouble expectedanswer = double.valueof ("1"); +Double actualanswer = double.valueof (Calcfunction.evaluate ("COS (0) =")); A assertequals (Expectedanswer, actualanswer); the     } +}
Calcfunctiontest.java

JUnit Test, Run as, results

The test shows that the green check mark is passed, showing the Blue Cross, indicating that the failure was not passed. The Testevaluate_cos test method in the.

Fails, the progress bar becomes reddish-brown, and there are two cases of failure:

When processing results with expected deviations (failure) and processing results, an exception is thrown directly-Test error (Error)

Two scenarios in which the test failed

When all tests are successful, the progress bar is green:

JUnit Best practices test for any possible errors. Unit tests are not meant to prove that you are right, but to prove that you are not wrong.

JUnit 4 Unit Test

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.