springboot入门(三)Springboot集成Junit单元测试

准备工作

编辑器:idea(非必须)
maven依赖(创建springboot自己会有):

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
</dependency>

spring-boot-starter-test 是springboot 1.4.0版本后在创建时候就会自动引入的一个测试依赖,无需我们在手动引入junit依赖,非常方便

JUnit中的注解
注解作用
@BeforeClass针对所有测试,只执行一次,且必须为static void
@Before始化方法,执行当前测试类的每个测试方法前执行
@Test测试方法,在这里可以测试期望异常和超时时间
@After释放资源,执行当前测试类的每个测试方法后执行
@AfterClass针对所有测试,只执行一次,且必须为static void
@Ignore略的测试方法(只在测试类的时候生效,单独执行该测试方法无效)
@RunWith可以更改测试运行器 ,缺省值 org.junit.runner.Runner

一个单元测试类执行顺序为:@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一个测试方法的调用顺序为:@Before –> @Test –> @After

测试方法注意事项

1.测试方法上必须使用@Test进行修饰
2.测试方法必须使用public void 进行修饰,不能带任何的参数
3. 测试单元中的每个方法必须可以独立测试,测试方法间不能有任何的依赖

测试实战:

springboot基础请看这里

  • 如何生成测试类:
    在idea要测试一个类只需要选中类名然后右键选中如图:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

ok完成即可!会生成相应的测试方法:

  • 测试详解:
package com.zou.sell.dao;

import com.zou.sell.dataobject.ProductCategory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;


/**
 * @Author: WH
 * @Date: 2019/4/29 22:29
 * @Version 1.0
 */
@RunWith(SpringRunner.class)
@SpringBootTest()
public class ProductCategoryRepositoryTest {

    @Autowired
    private ProductCategoryRepository repository;
	
    @Test
    public void findOneTest() {
        ProductCategory productCategory = repository.findById(1).orElse(null);
    }
	//表示这是一个测试方法
    @Test
    public void saveTest1() {
        ProductCategory productCategory = new ProductCategory();
        productCategory = repository.findById(2).orElse(null);
        productCategory.setCategoryType(10);
        repository.save(productCategory);
    }

    @Test
    //事务回滚,加入这个注解测试写入数据的数据就不会写入数据库
    @Transactional
    public void saveTest() {
        ProductCategory productCategory = new ProductCategory("女生最爱",5);
        ProductCategory result = repository.save(productCategory);
        //返回结果result如果不为空返回true
        Assert.assertNotNull(result);
//        Assert.assertNotEquals(null, result);
    }

    @Test
    public void findByCategoryTypeInTest() {
        List<Integer> list = Arrays.asList(2,3,10);

        List<ProductCategory> result = repository.findByCategoryTypeIn(list);
        //返回结果result不为result.size()不为0则返回true
        Assert.assertNotEquals(0, result.size());
    }

}

补充说明:
Assert中的更多方法可以自己去了解:
在这里插入图片描述
在这里插入图片描述

可以看到加了 @Test注解的方法都可以单独运行,如果要测试全部方法可以直接运行这个类
这是测试成功的标志:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值