Files
rikako-note/spring/spring boot/UnitTest.md
2023-01-10 21:59:13 +08:00

49 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Spring单元测试
## SpringBootTest
在SpringBoot中提供了@SpringBootTest注解。当需要SpringBoot特性时可以通过使用@SpringBootTest注解来作为@ContextConfiguration的替代@SprngBootTest创建ApplicationContext该context在test中被使用。
## @AfterAll
该注解标明的方法会在所有Test执行完成之后再执行
> ### @AfterAll注解特性
> - @AfterAll注解的方法必须含有void类型返回值
> - @AfterAll注解标注方法不能为private
> - 默认情况下,@AfterAll注解的方法必须是static修饰的
> ### 在非static方法中标注@AfterAll
> 在非static方法上标注@AfterAll需要在class上标注@TestInstance(TestInstance.Lifecycle.PER_CLASS)。
> 因为默认情况下TestInstance的默认生命周期是PER_METHOD
> ```JAVA
> @TestInstance(TestInstance.Lifecycle.PER_CLASS)
> public class BeforeAndAfterAnnotationsUnitTest {
>
> String input;
> Long result;
> @BeforeAll
> public void setup() {
> input = "77";
> }
>
> @AfterAll
> public void teardown() {
> input = null;
> result = null;
> }
>
> @Test
> public void whenConvertStringToLong_thenResultShouldBeLong() {
> result = Long.valueOf(input);
> Assertions.assertEquals(77l, result);
> }
> }
> ```
## @AfterEach
该注解标明的方法,在每次@Test标注方法执行完成之后都会被执行
> ### @AfterEach特性
> - 标注方法返回值为空
> - 标注方法不为private
> - 标注方法不是static
## @BeforeAll
类似@AfterAll
## @BeforeEach
类似@AfterEach