在Spring Boot項(xiàng)目中使用JUnit參數(shù)化測試可以幫助我們更快速地進(jìn)行測試,減少重復(fù)的測試代碼編寫。以下是一個簡單的示例,演示如何在Spring Boot項(xiàng)目中使用JUnit參數(shù)化測試:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
@RunWith(JUnitParamsRunner.class)
注解:import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
@RunWith(JUnitParamsRunner.class)
public class ParameterizedTest {
@Test
@Parameters({"1, 2, 3", "2, 3, 5", "3, 4, 7"})
public void testAdd(int a, int b, int expectedResult) {
Calculator calculator = new Calculator();
assertEquals(expectedResult, calculator.add(a, b));
}
}
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
通過上面的步驟,我們就可以在Spring Boot項(xiàng)目中使用JUnit參數(shù)化測試了。參數(shù)化測試可以大大簡化我們的測試代碼編寫,提高測試的效率和可維護(hù)性。