springBoot单元测试-基础单元测试

1)在pom文件中加入junit支持

1     <!-- spring-boot-starter-test 单元测试 -->
2     <dependency>
3         <groupId>org.springframework.boot</groupId>
4         <artifactId>spring-boot-starter-test</artifactId>
5         <scope>test</scope>
6     </dependency>

2)新建测试类 Application1Test

在class 头上增加注解

@RunWith(SpringRunner.class)  :指定运行平台,点进去 SpringRunner.class 可以看到他是集成自 SpringJUnit4ClassRunner 
@SpringBootTest : 标记此类为方法测试类,在加载和其他扫描是spring不会去对他进行扫描

 

 1 @RunWith(SpringRunner.class) 
 2 @SpringBootTest
 3 public class Application1Test {
 4 
 5     
 6     @Test
 7     public void test1() {
 8         
 9         //业务逻辑代码
10         System.err.println("this is  test1 !");
11         
12     }
13     @Test
14     public void test2() {
15         
16         //业务逻辑代码
17         System.err.println("this is  test2 !");
18         
19     }
20     
21     // @Before 标记 在执行Test 前执行该方法
22     @Before
23     public void testBefore() {
24         
25         //业务逻辑代码
26         System.err.println("this is  testBefore !");
27         
28     }
29     
30     // @After 标记 在执行Test 后执行该方法
31     @After
32     public void testAfter() {
33         
34         //业务逻辑代码
35         System.err.println("this is  testAfter !");
36         
37     }
38     
39 
40 
41 }

 

认为 springBoot的实际运用场景并不是很多,但可以用在像计算一段复杂代码的执行时间,比如数据库连接耗时等需求上

 

posted @ 2018-07-13 11:29  陈先生的小板凳  阅读(534)  评论(0编辑  收藏  举报