JUnit Learning Notes-idea installation and use of JUnit in the compiler

Source: Internet
Author: User
Tags assert mul

One, what is JUnit
JUnit is a unit-testing framework for the Java language. A unit test is a white-box test that tests the functional modules of the code (a single feature).
Two, IntelliJ idea installs the JUnit plug-in
1. Select the settings for the File menu ... Options

2. Choose Settings ... Browse repositories options within the Plguins option in the interface

3. Enter JUnit in the search box and choose JUnit Generator V2.0 Installation from the menu below

(because I have already installed, so does not show install option)
4. Installation completed, in the class to be tested by the generation method shortcut key Alt+ins, the Jnittest method appears.

Iii. Methods for unit testing using JUnit
1. Generate Unit Test class
Use the shortcut key Alt+ins menu in the class to be tested to select JUnit TEST-->JUNIT4 to automatically generate the test class.

To solve the coding problem, click Reload in another encoding (red oval mark) to select the default encoding (GBK)

Resolving the annotation does not recognize the problem, at this point the annotation is cannot resolve state, because JUNIT4 is not joined Classpath of the project, use Alt+enter shortcut key to add JUNIT4 to Classpath

2. Methods and notes in the Unit test class
• All methods must have no return value (public void) and no parameters
• @Test annotations
Demonstrates that this method is a unit test method that can be performed separately to implement the test functionality.
• @Before annotations
This method is executed before each unit test method is run.
• @After annotations
This method is executed after each unit test method finishes execution.
• @BeforeClass annotations (method must be static)
This method is executed before all unit test methods are executed.
• @AfterClass annotations (method must be static)
Executes this method after all unit test methods have been executed.
• @Ignore annotations
Ignores this method, ignoring this method (not executing) when the test class is executed.
Methods in the 3.Assert (assertion) class implement automated results checking
. Assertequals (Expected,actual) determines whether expectations are the same as the actual values
. Assertnull (object) determines whether it is empty
. Assertnotnull (object) determines whether it is not empty
. Asserttrue (condition) determines whether a Boolean value is True
. Assertfalse (condition) determines whether the Boolean value is False
Iv. Specific examples
Calculator class code to be tested: (Implement subtraction function)

public class Calculator {public
    int add (int a,int b) {return a+b;}
    public int sub (int a,int b) {return a-b;}
    public int mul (int a,int b) {return a*b;}
    public int div (int a,int b) {
        if (b==0) {
            throw new runtimeexception ("divisor cannot be 0");
        return a/b.}

1. Test the Add method (in the Calculatortest Class)

/** 
* METHOD:ADD (int a, int b) */ 
@Test public
void Testadd () throws Exception { 
    int a = 5; int b = 7; int exp =;
    Assert.assertequals (exp, new Calculator (). Add (a,b));

When the Testadd method name is selected, the right mouse button selects run ' Testadd () ' To run this method, and the results of the operation are as follows: (the test is shown as green by the actual value, and if the test is not passed, it is red)

2. Testing multiple methods

public class Calculatortest {@BeforeClass public static void Beforeclass () throws exception{System.out.println ("Befo
Reclass "); @AfterClass public static void Afterclass () throws exception{System.out.println ("Afterclass");} @Before public Voi D before () throws Exception {System.out.println ("before");} @After public void after () throws Exception {System
. OUT.PRINTLN ("after");  }/** * Method:add (int a, int b) */@Test public void Testadd () throws Exception {//todo:test goes ... int A = 5; int B = 7;
    int exp = 12;
Assert.assertequals (exp, new Calculator (). Add (a,b));  }/** * METHOD:SUB (int a, int b) */@Test public void TestSub () throws Exception {//todo:test goes ... int A = 15; int B = 7;
    int exp = 8;
Assert.assertequals (exp, new Calculator (). Sub (a,b));  }/** * Method:mul (int a, int b) */@Test public void Testmul () throws Exception {//todo:test goes ... int A = 5; int B = 7;
    int exp = 35; Assert.assertequaLS (exp, new Calculator (). Mul (a,b));  }/** * METHOD:DIV (int a, int b) */@Test public void Testdiv () throws Exception {//todo:test goes ... int A = 5; int b = 1;
    int exp = 5;
Assert.assertequals (exp, new Calculator (). Div (a,b)); 
 } 
}

Run Test class Results

The four methods that are tested by the results are all passed, and the method of the Beforeclass annotation is executed at the very beginning, the method of Afterclass annotation is executed at the end of all tests, and the method of before annotation is executed before each test begins. The method after the annotation is executed at the end of each test.
4. Testing illegal data
To set the divisor B in the Testdiv method to illegal data 0, the results are as follows

Show Testdiv not passed, throw runtimeexception. (garbled reason for this place to choose the encoding is GBK, use UTF-8 can be solved)
To add an expected exception to the note parameter test:

@Test (expected = java.lang.RuntimeException.class) public
void Testdiv () throws Exception { 
//todo:test goes
    here ... int a = 5; int b = 0; int exp = 5;
    Assert.assertequals (exp, new Calculator (). Div (a,b));

The exception that is expected to be thrown is added here to the parameters of the annotation @test, and the results are rerun as follows

The test passes because the exception caused by the error data is the expected exception.

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.