首页 > 编程语言 > 详细

springboot junit test

时间:2020-01-02 13:24:16      阅读:75      评论:0      收藏:0      [点我收藏+]

1.注解列表

  • @RunWith:标识为JUnit的运行环境;
  • @SpringBootTest:获取启动类、加载配置,确定装载Spring Boot;
  • @Test:声明需要测试的方法;
  • @BeforeClass:针对所有测试,只执行一次,且必须为static void;
  • @AfterClass:针对所有测试,只执行一次,且必须为static void;
  • @Before:每个测试方法前都会执行的方法;
  • @After:每个测试方法前都会执行的方法;
  • @Ignore:忽略方法;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})

 

2.Assert断言

  • Assert.assertEquals 对比两个值相等
  • Assert.assertNotEquals 对比两个值不相等
  • Assert.assertSame 对比两个对象的引用相等
  • Assert.assertArrayEquals 对比两个数组相等
  • Assert.assertTrue 验证返回是否为真
  • Assert.assertFlase 验证返回是否为假
  • Assert.assertNull 验证null
  • Assert.assertNotNull 验证非null

 

3.Web模拟测试

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestServiceImplTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void test() {
        restTemplate.getForObject(xx, xx)
    }

} 

 

 

4.数据库测试

给测试类上添加“@Transactional”,这样既可以测试数据操作方法,又不会污染数据库了。

 

Spring Boot(十二)单元测试JUnit

https://www.cnblogs.com/vipstone/p/9908545.html

JUnit基本注解使用

https://www.jianshu.com/p/921282034c5d

https://blog.csdn.net/qq_42651904/article/details/89945495

springboot junit test

原文:https://www.cnblogs.com/foolash/p/12131940.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!