JUnit unit testing -- IntelliJ IDEA

Basic use of unit testing

1. Environment configuration

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

          1. Install the JUnit plugin steps

              File-->settings-->Plguins-->Browse repositories-->Enter JUnit-->Select JUnit Generator V2.0 to install.

          2. Use the JUnit plugin

             In the class that needs to be unit tested, use the shortcut key alt+insert, select JUnit test, and select JUnit4.

2. Unit testing

Code Demo:

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

1> Notes:

    1. The test method must be decorated with the @Test annotation.

    2. The test method must be modified 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 tested class.

    5. Each method in the test unit must be tested independently, and there can be no dependencies between each test method.

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

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

2> Error parsing:

    1. Failure is generally caused by the failure of the assertion method used in unit testing, indicating that the expected result is inconsistent with the program running result.

    2. The error is caused by a code exception, which is caused by a bug in the test code itself.

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

3>测试流程:

代码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所修饰的方法在所有方法加载前执行,而且他是静态的在类加载后就会执行该方法,

         在内存中只有一份实例,适合用来加载配置文件。

      2、@AfterClass所修饰的方法在所有方法执行完毕之后执行,通常用来进行资源清理,例如关闭数据库连接。

      3、@Before和@After在每个测试方法执行前都会执行一次。

4>常用注解

      1、@Test(excepted=XX.class) 在运行时忽略某个异常。

      2、@Test(timeout=毫秒) 允许程序运行的时间。

      3、@Ignore 所修饰的方法被测试器忽略。

      4、RunWith 可以修改测试运行器 org.junit.runner.Runner

5>测试套件

      测试套件是组织测试类一起运行的测试类。具体如下:

代码Demo:

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

  注意事项:

      1、作为测试套件的入口类,类中不能包含任何方法。

      2、更改测试运行器Suite.class。

      3、将需要运行的测试类放入Suite.SuiteClasses({})的数组中。

6>参数化设置

      需要测试的仅仅是测试数据,代码结构是不变的,只需要更改测试数据。

代码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));
    }

}
复制代码

    具体步骤:

        1、更改默认的测试运行器为@RunWith(Parameterized.class)。

        2、声明变量来存放预期值和测试值。

        3、声明一个返回值为Collection的公共静态方法,并用@Parameters修饰。

        4、为测试类声明一个带有参数的公共构造函数,并在其中为他声明变量赋值。

以上为基于IntelliJ IDEA 进行的单元测试。       


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839562&siteId=291194637