引入相关依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
配置相关注解
package net.cyb.demo; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = {DemoProject1Application.class}) public class VideoTest { @Before public void testOne(){ System.out.println("这个是测试Before"); } @Test public void testTwo(){ System.out.println("这个是测试Test"); } @After public void testThree(){ System.out.println("这个是测试After"); } }
常用单元测试的注解
- @Before
- @Test
- @After
断言
判断程序结果是否符合预期TestCase.assertxxxx()