Junit's two common test methods -- Basic test and parametric test

If you don't know how idea creates Junit5 test project, you can see it This blog

Test process

Basic test

  • Annotate the Test method @ Test

shortcoming

Each method can only be tested. To test multiple use cases in a use case, many methods need to be added, resulting in very bloated code, which is not recommended

package com.xjtu;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CalculatorTest {
    Calculator cal;

    @BeforeEach
    void setUp() {
        cal = new Calculator();
    }

    @AfterEach
    void tearDown() {
        cal = null;
    }

    @Test
    void add1() {
        assertEquals(30, cal.add(10, 20));
    }

    @Test
    void add2() {
        assertEquals(0, cal.add(0, 0));
    }

    @Test
    void add3() {
        assertEquals(-10, cal.add(10, -20));
    }

    @Test
    void sub1() {
        assertEquals(-10, cal.sub(10, 20));
    }

    @Test
    void sub2() {
        assertEquals(10, cal.sub(20, 10));
    }

    @Test
    void sub3() {
        assertEquals(-20, cal.sub(0, 20));
    }

    @Test
    void multiply1() {
        assertEquals(20, cal.multiply(1, 20));
    }

    @Test
    void multiply2() {
        assertEquals(-20, cal.multiply(1, -20));

    }

    @Test
    void multiply3() {
        assertEquals(40, cal.multiply(-2, -20));

    }

    @Test
    void division1() {
        assertEquals(10, cal.division(20, 2));
    }

    @Test
    void division2() {
        assertEquals(-10, cal.division(20, -2));
    }

    @Test
    void division3() {
        assertEquals(0, cal.division(0, 2));
    }
}

Parametric test

  • Annotate the test method @ parametrizedtest
  • Use the annotation @ xSource to formulate test data, where: x may be Value, Enum, Method, Csv, CsvFile, Arguments, etc

Add your own unique test data for each test method

package com.xjtu;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CalculatorParameterizedTest {
    Calculator cal = new Calculator();

    @BeforeEach
    void setUp() {
    }

    @AfterEach
    void tearDown() {
    }

    @ParameterizedTest
    @CsvSource({"10,20,30", "0,0,0", "10,-20,-10"})
    void add(ArgumentsAccessor args) {
        assertEquals(args.getInteger(2), cal.add(args.getInteger(0), args.getInteger(1)));
    }

    @ParameterizedTest
    @CsvSource({"10,20,-10", "20,10,10", "0,20,-20"})
    void sub(ArgumentsAccessor args) {
        assertEquals(args.getInteger(2), cal.sub(args.getInteger(0), args.getInteger(1)));
    }

    @ParameterizedTest
    @CsvSource({"1,20,20", "1,-20,-20", "-2,-20,40"})
    void multiply(ArgumentsAccessor args) {
        assertEquals(args.getInteger(2), cal.multiply(args.getInteger(0), args.getInteger(1)));
    }

    @ParameterizedTest
    @CsvSource({"20,2,10", "20,-2,-10", "0,2,0"})
    void division(ArgumentsAccessor args) {
        assertEquals(args.getInteger(2), cal.division(args.getInteger(0), args.getInteger(1)));
    }
}

Use Kit

In the actual project, with the development of the project progress, there will be more and more unit test classes, but until now, we only run test classes one by one, which is certainly not feasible in the actual project practice. To solve this problem, JUnit provides a method to run test classes in batch, called test suite.

In this way, each time you need to verify the correctness of the system function, only one or several test suites can be executed.

Because JUnit 5 is used, I searched the Internet for a long time and didn't find the use method of the suite. After A blog You can right-click the package name and select run Tests in

This package, you can execute all test classes in the package

The final implementation results are as follows

JUnit Usage Summary

  • Instead of initializing with the constructor of Testcase, use @ BeforeXxx and @ AfterXxx
  • Don't rely on or assume the order of test run, because JUnit will use Vector to save test methods, so different platforms will take test data from Vector in different order, which is taken out and executed according to the alphabetical order under idea
  • When integrating a test class, remember to call the classified @ BeforeXxx and @ AfterXxx
  • Put the test code and working code together to compile and update synchronously
  • Test classes and test methods should have consistent naming schemes. For example, add test after the class description to form a test class
  • It is possible to use the assert and fail methods and exception handling methods provided by JUnit, which can make the code more brief

Keywords: Java IntelliJ IDEA Junit Testing

Added by mikejs on Sat, 15 Jan 2022 23:57:27 +0200