在springboot中我们写测试类测试service时比较容易,但当我们测试controller时一般用的是postman,本文我将介绍如何使用测试类测试controller
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
测试service类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTests {
@Autowired
private UserService userService;
@Test
public void testAddUser() {
User user = new User();
user.setName("john");
user.setAddress("earth");
userService.add(user);
}
}
SpringBoot测试controller类
import l