SpringBoot整合junit


SpringBoot整合junit

主要分为4步

  1.  添加依赖
  2. 创建测试类
  3. 在测试类上添加注解
  4. 在测试类注入测试对象

1:导入依赖包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

 

 

 

 

 

2:创建测试类

3:在测试类上添加注解并注入测试对象

测试类上注解:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springbootdemo1Application.class)
package com.offcn.springbootdemo1;

import com.offcn.springbootdemo1.mapper.UUserMapper;
import com.offcn.springbootdemo1.pojo.UUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

//添加整合junit注解
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springbootdemo1Application.class)
public class AppTest {
   //注入Mapper对象,用来获取List<UUser>集合

@Resource
private UUserMapper userMapper;
@Test
public void TestMethod(){
List<UUser> uUsers = userMapper.selectUUser();
for (UUser uUser : uUsers) {
System.out.println(uUser);
}
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM