JUnit unit testing using IntelliJ IDEA

Reprinted: https://www.cnblogs.com/huaxingtianxia/p/5563111.html

The basic unit testing using

First, the environment configuration

 Using the unit test idea IDE, first need to install JUnit plug.

Step 1. Install JUnit plug

   File -> settings -> Plguins -> Browse repositories -> input JUnit -> select JUnit Generator V2.0 installation.

2. Using JUnit Plug

   The classes are to be tested unit, use the shortcut alt + insert, select JUnit test, select JUnit4.

Second, the test unit

Code Demo:

  @Test
    public void testAdd() {
        assertEquals(2, new UserDao().add(1, 1));
    }

1> Note:

    1, the test method described above must be modified @Test comment.

    2, the test method must use public void modified, can not have any arguments.

    3, a new source code directory used to store test code.

    4, the test packet class should be consistent with the type of the test package.

    5, each test unit independent testing method must not have dependencies between each test method.

    6, the test class uses Test as the class name suffix (non-essential).

    7, test methods using the prefix test as the method name (non-essential).

2> Error Analysis:

    1, Failure test unit assertion methods generally used due to failure determination described inconsistency expected results and the results of running the program.

    2, error code that is caused by abnormal, he produced in the test code itself the Bug.

    3, test cases are not used to prove you're right, but to prove that you are not wrong.

3> testing process:

Code Demo:    

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {

    }
    @AfterClass
    public static void setUpAfterClass() throws Exception {

    }

    @Before
    public void before() throws Exception {

    }

    @After
    public void after() throws Exception {

    }

      1, @ BeforeClass the modified method of performing in front of all the methods to load, and he will be executed after the static method of class load,

         Only one instance in memory, suitable for loading a configuration file.

      2, @ AfterClass the modified method is performed after all the method is finished, the resource is typically used for cleaning, for example, close the connection.

      3, @ Before and @After execution will be performed once before each test method.

4> Common Annotations

      1, @ Test (excepted = XX.class) ignore an exception at run time.

      2, @ Test (timeout = ms) to allow the time to run the program.

      3, @ Ignore the modified method is ignored tester.

      4, RunWith can modify the test runner org.junit.runner.Runner

5> test suite

      Test Suite is a test class to run with tissue test class. details as follows:

Code Demo:

@RunWith(Suite.class)
@Suite.SuiteClasses({UserTest1,UserTest2,UserTest3})
public class SuiteTest{
    
}

  Precautions:

      1, as an inlet of the test suite class, the class must not include any method.

      2, change the test runner Suite.class.

      3, the test to be run into the class Suite.SuiteClasses ({}) in the array.

6> parameterization

      Need to test only test data and code structure is the same, only need to change the test data.

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 for the @RunWith (Parameterized.class).

2, declare a variable to store the expected values ​​and test values.

3. Collection declare a return value of public static method, and modified by @Parameters.

4, as a test class declare a public constructor with parameters, and in which assigned him to declare variables.

The above is based on the unit test performed IntelliJ IDEA.       

Guess you like

Origin www.cnblogs.com/geduocoding/p/11303567.html