欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot做单元测试

程序员文章站 2022-04-29 16:38:25
...

1.配置maven依赖pom.xml

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

2.测试类

package com.xxxx.controller;

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 com.xxxx.entity.xxxx;
import com.xxxx.service.xxxService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JunitTest1 {
	
	@Autowired
	private xxxService xxService;
	
	@Test
    public void doTest() {
       xxxx x= new xxxx();
       x.setName("单元测试");
       xxService.insertxxxx(x);
    }
	
}

单击右键运行Junit Test,Junit视图里可以看见运行结果,如果有错误,则查看Failure Trace里面的错误信息即可

相关标签: spring boot