引入maven 配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
编写测试类:
import com.fen.dou.service.TestJunitService;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestJunit {
@Autowired
private TestJunitService testJunitService;
@Test
public void testSay(){
testJunitService.say();
}
}
其实idea有一个快捷键,在你需要单元测试的service类里面,按Ctrl+Shift+T,勾选你需要测试的方法,就可以生成测试类了