准备工作

编辑器: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. 测试单元中的每个方法必须可以独立测试,测试方法间不能有任何的依赖

测试实战:

  • 如何生成测试类:
    在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中的更多方法可以自己去了解:

spring boot controller测试 springboot的test_springboot


spring boot controller测试 springboot的test_单元测试_02

可以看到加了 @Test注解的方法都可以单独运行,如果要测试全部方法可以直接运行这个类

这是测试成功的标志:

spring boot controller测试 springboot的test_单元测试_03