idea生成springboot单元测试用例

1、找到需要生成单元测试的类型,右键Go To -> Test

 2、选择JUnit4 和勾选需要测试的方法

 3、查看自动生成的文件


public class CategoryMapperTest {

    @Test
    public void findById() {
    }

    @Test
    public void queryById() {
    }
}

4、添加测试代码

@RunWith(SpringRunner.class)
@SpringBootTest
public class CategoryMapperTest {

    @Autowired
    private CategoryMapper categoryMapper;

    @Test
    public void findById() {
        Category category = categoryMapper.findById(100001);
        System.out.println(category.toString());
    }

    @Test
    public void queryById() {
        Category category = categoryMapper.queryById(100001);
        System.out.println(category.toString());
    }
}

注意:@RunWith(SpringRunner.class)
           @SpringBootTest

这两行让项目跑起来后运行测试用例,必须加上

方法二:通过继承applicationTest,

public class CategoryMapperTest extends MallApplicationTests