Spring Boot整合Junit
在Spring Boot项目中使用Junit进行单元测试UserService的方法
1.添加启动器依赖spring-boot-starter-test
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
2.编写测试类
mac环境 在UserService名称 上 按住command+shift+t 点击ok
package com.test.service;
import com.test.been.User;
import org.junit.jupiter.api.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
class UserServiceTest {
@Autowired
private UserService userService;
@Test
void queryById() {
User user= userService.queryById(1);
System.out.println("user"+user);
}
@Test
void addUser() {
User user=new User();
user.setName("test");
user.setUsername("test");
user.setPassword("1234");
user.setSex("男");
user.setAge(14);
user.setIsDelete(0);
user.setPermission("允许");
userService.addUser(user);
}
}