在springboot中进行单元测试,大家已经非常熟悉。我们通常测试的是service层和dao层。
对controller层的直接测试可能进行的较少。
下面介绍一下在SpringBoot中进行Controller层的Rest请求测试的方法。
还是使用我之前的一个rest请求
第一种方法:
@RunWith(SpringRunner.class)// 随机创建出一个端口测试@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)public class HelloControllerTest { @Autowired private TestRestTemplate restTemplate; @Test public void sayHello() throws Exception { ResponseEntity response = this.restTemplate.getForEntity("/hello