Using JUnit under Java:idea

Source: Internet
Author: User

Basic use of unit testing environment configuration

Using the idea IDE for unit testing, you first need to install the JUnit plugin.

1. Installing the JUnit plug-in steps

File-->settings-->plguins-->browse repositories--> input junit--> Select JUnit Generator V2.0 installation.

2. Using the JUnit plugin

In the class that requires unit testing, use the shortcut key Alt+insert, select JUnit test, and select JUNIT4.

Second, Unit testing

Code Demo:

@Test public    void Testadd () {        assertequals (2, New Userdao (). Add (1, 1));    }

1> Precautions:

1. The test method must be decorated with @test annotations.

2. The test method must be decorated with public void and cannot have any parameters.

3. Create a new source code directory to store the test code.

4. The package of the test class should be consistent with the package of the class being tested.

5. Each method in the test unit must be tested independently and cannot be relied upon between each test method.

6. Test class uses test as the suffix of the class name (not necessary).

7. The test method uses test as the prefix for the method name (not necessary).

2> Error parsing:

1, Failure is generally a unit test used by the assertion method to determine the failure, indicating that the expected results and program running results are inconsistent.

2, error is caused by a code exception, he generated in the test code itself in the bug.

3. Test cases are not used to prove that you are right, but to prove that you are not wrong.

3> Test Flow:

Code Demo:

    @BeforeClass public    static void Setupbeforeclass () throws Exception {    }    @AfterClass public    static void Setupafterclass () throws Exception {    }    @Before public    Void before () throws Exception {    }    @After C12/>public void After () throws Exception {    }

1. The method that @BeforeClass modifies is executed before all methods are loaded, and he is static and executes the method after the class is loaded.

There is only one instance in memory that is suitable for loading configuration files.

2. @AfterClass methods are executed after all methods have been executed, typically for resource cleanup, such as closing a database connection.

3. @Before and @after are executed once before each test method executes.

4> frequently used annotations

1. @Test (Excepted=xx.class) ignores an exception at run time.

2, @Test (timeout= milliseconds) allows the program to run for a time.

3. The method modified by @Ignore is ignored by the tester.

4. Runwith can modify the test runner Org.junit.runner.Runner

5> Test Kit

Test suites are test classes that run with the Organization test class. Specific as follows:

Code Demo:

@RunWith (Suite.class) @Suite. suiteclasses ({USERTEST1,USERTEST2,USERTEST3}) public class suitetest{    }

Precautions:

1. As the entry class for the test suite, the class cannot contain any methods.

2. Change the test runner Suite.class.

3. Put the test class you want to run into an array of suite.suiteclasses ({}).

6> parameterization Settings

The only test data that needs to be tested is that the code structure is unchanged, and only the test data needs to be changed.

Code Demo:

@RunWith (parameterized.class) public class Parametertest {    int expected = 0;    int input1 = 0;    int input2 = 0;    @Parameters public    static collection<object[]> T () {        return arrays.aslist (new object[][]{                {3,1,2},                {5,2,3}        });    }    Public parametertest (int expected,int input1,int input2) {        this.expected = expected;        THIS.INPUT1 = INPUT1;        This.input2 = Input2;    }    @Test public    void Testadd () {        assertequals (expected, Userdao.add (INPUT1,INPUT2));}    }

Specific steps:

1. Change the default test runner to @runwith (Parameterized.class).

2. Declare variables to hold the expected values and test values.

3. Declare a public static method with a return value of collection, and modify it with @parameters.

4. Declare a public constructor with parameters for the test class and assign a value to the variable that he declares.

The above is a unit test based on IntelliJ idea.

Using JUnit under Java:idea

Related Article

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.