该栏目会系统的介绍SpringBoot的知识体系,共分为基础部分、源代码和综合实例等模块
简介
1、概述
:单元测试(英语:Unit Testing)又称为模块测试, 是针对程序模块来进行正确性检验的测试工作
2、常用注解
描述 注解 测试方法 @Test
为测试类或者测试方法设置展示名称 @DisplayName
参数化测试方法 @ParameterizedTest
可重复执行方法 @RepeatedTest
每个单元测试之前执行 @BeforeEach
每个单元测试之后执行 @AfterEach
在所有单元测试之前执行 @BeforeAll
所有单元测试之后执行 @AfterAll
单元测试类别 @Tag
测试类或测试方法不执行 @Disabled
方法运行如果超过了指定时间将会返回错误 @Timeout
测试类或测试方法提供扩展类引用 @ExtendWith
3、整合 Junit
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
</ dependency>
@RunWith ( value = SpringRunner . class )
@SpringBootTest ( classes = MainApplication . class )
public class TestDemo {
@Autowired
private Person person;
@Test
public void readPersonTest ( ) {
System . out. println ( person. toString ( ) ) ;
}
}
断言
1、概述
:断言是一种测试机制,它可以规定某个参数或者属性必须要满足某个条件,否则会抛出一个异常,并且程序会中止
2、简单断言
描述 方法名 判断两个对象或两个原始类型是否相等 assertEquals
判断两个对象或两个原始类型是否不相等 assertNotEquals
判断两个对象引用是否指向同一个对象 assertSame
判断两个对象引用是否指向不同的对象 assertNotSame
判断给定的布尔值是否为 true assertTrue
判断给定的布尔值是否为 false assertFalse
判断给定的对象引用是否为 null assertNull
判断给定的对象引用是否不为 null assertNotNull
@Test
@DisplayName ( "simple assertion" )
public void simple ( ) {
assertEquals ( 3 , 1 + 2 , "simple math" ) ;
assertNotEquals ( 3 , 1 + 1 ) ;
assertNotSame ( new Object ( ) , new Object ( ) ) ;
Object obj = new Object ( ) ;
assertSame ( obj, obj) ;
assertFalse ( 1 > 2 ) ;
assertTrue ( 1 < 2 ) ;
assertNull ( null ) ;
assertNotNull ( new Object ( ) ) ;
}
3、数组断言
@Test
@DisplayName ( "array assertion" )
public void array ( ) {
assertArrayEquals ( new int [ ] { 1 , 2 } , new int [ ] { 1 , 2 } ) ;
}
4、组合断言
@Test
@DisplayName ( "assert all" )
public void all ( ) {
assertAll ( "Math" ,
( ) -> assertEquals ( 2 , 1 + 1 ) ,
( ) -> assertTrue ( 1 > 0 )
) ;
}
5、异常断言
@Test
@DisplayName ( "异常测试" )
public void exceptionTest ( ) {
ArithmeticException exception = Assertions . assertThrows (
ArithmeticException . class , ( ) -> System . out. println ( 1 % 0 ) ) ;
}
6、超时断言
@Test
@DisplayName ( "超时测试" )
public void timeoutTest ( ) {
Assertions . assertTimeout ( Duration . ofMillis ( 1000 ) , ( ) -> Thread . sleep ( 500 ) ) ;
}
7、快速失败
@Test
@DisplayName ( "fail" )
public void shouldFail ( ) {
fail ( "This should fail" ) ;
}
前置条件(Assumption)
1、概述
:类似于断言,不同之处在于不满足的断言会使得测试方法失败,而不满足的前置条件只会使得测试方法的执行终止
2、案例
@DisplayName ( "前置条件" )
public class AssumptionsTest {
@Test
public void simpleAssume ( ) {
Assumptions . assumeTrue ( true , "结果不是true" ) ;
System . out. println ( "前置条件满足..." ) ;
}
}
嵌套测试
1、概述
:通过 Java 中的内部类和 @Nested 注解实现嵌套测试,从而可以更好的把相关的测试方法组织在一起
2、案例
@DisplayName ( "A stack" )
class TestingAStackDemo {
Stack < Object > stack;
@Test
@DisplayName ( "is instantiated with new Stack()" )
void isInstantiatedWithNew ( ) {
new Stack < > ( ) ;
}
@Nested
@DisplayName ( "when new" )
class WhenNew {
@BeforeEach
void createNewStack ( ) {
stack = new Stack < > ( ) ;
}
@Test
@DisplayName ( "is empty" )
void isEmpty ( ) {
assertTrue ( stack. isEmpty ( ) ) ;
}
@Test
@DisplayName ( "throws EmptyStackException when popped" )
void throwsExceptionWhenPopped ( ) {
assertThrows ( EmptyStackException . class , stack:: pop ) ;
}
@Test
@DisplayName ( "throws EmptyStackException when peeked" )
void throwsExceptionWhenPeeked ( ) {
assertThrows ( EmptyStackException . class , stack:: peek ) ;
}
}
}
参数化测试
1、概述
:使用不同的参数进行多次单元测试,而不需要每新增一个参数就新增一个单元测试,省去了很多冗余代码
2、相关注解
描述 注解 指定参数来源,比如八大基础类、String类型和Class类型 @ValueSource
指定 null 值 @NullSource
指定枚举类型 @EnumSource
指定 CSV 文件内容 @CsvFileSource
读取指定方法的返回值 @MethodSource
3、案例
@ParameterizedTest
@ValueSource ( strings = { "one" , "two" , "three" } )
@DisplayName ( "参数化测试" )
public void parameterizedTest1 ( String string) {
System . out. println ( string) ;
Assertions . assertTrue ( StringUtils . isNotBlank ( string) ) ;
}
@ParameterizedTest
@MethodSource ( "method" )
@DisplayName ( "方法来源参数" )
public void testWithExplicitLocalMethodSource ( String name) {
System . out. println ( name) ;
Assertions . assertNotNull ( name) ;
}
static Stream < String > method ( ) {
return Stream . of ( "apple" , "banana" ) ;
}