怎么做网站栏目,义乌品牌网站建设,安徽省建设部网站官网,国际公司名字文章目录 Junit注解常用类无参数单测带参数的单测 Junit
主要版本有4和5版本#xff0c;注解不太一样#xff0c; 4迁移5参考官方文档
主要记录下常用的一些操作
其他复杂操作见官网
https://junit.org/junit5/docs/current/user-guide/#overview-java-versions
引入5.9… 文章目录 Junit注解常用类无参数单测带参数的单测 Junit
主要版本有4和5版本注解不太一样 4迁移5参考官方文档
主要记录下常用的一些操作
其他复杂操作见官网
https://junit.org/junit5/docs/current/user-guide/#overview-java-versions
引入5.9.3版本依赖dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter/artifactIdversion5.9.3/versionscopetest/scope/dependency
test包下使用
注解
BeforeAll只执行一遍在一个类里所有方法执行前执行AfterAll只执行一遍在一个类里所有方法执行后执行BeforeEach每个方法执行前执行AfterEach每个方法执行后执行Test适用无返回值无参数的方法标记为单测Disabled标识该方法不执行DisplayName标识该单测方法别名Order指定执行顺序Nested放在类上用来在一个测试类里嵌套测试类时使用RepeatedTest()放在方法上指定执行次数ParameterizedTest指定带参数的方法ValueSource和上面配合使用Timeout指定超时时间
常用类
Assertions类 单纯判断结果是否符合预期
Assumptions类 可以指定抛出信息无参数单测
class StandardTests {BeforeAllstatic void initAll() {}BeforeEachvoid init() {}Testvoid succeedingTest() {}Testvoid failingTest() {fail(a failing test);}TestDisabled(for demonstration purposes)void skippedTest() {// not executed}Testvoid abortedTest() {assumeTrue(abc.contains(Z));fail(test should have been aborted);}AfterEachvoid tearDown() {}AfterAllstatic void tearDownAll() {}}带参数的单测
1.基本参数类型
ParameterizedTest
ValueSource(strings { racecar, radar, able was I ere I saw elba })
void palindromes(String candidate) {assertTrue(StringUtils.isPalindrome(candidate));
}2.方法入参
ParameterizedTest
MethodSource(stringProvider)
void testWithExplicitLocalMethodSource(String argument) {assertNotNull(argument);
}static StreamString stringProvider() {return Stream.of(apple, banana);
}