controller 和dao_[易學springboot]對controller層進行單元測試
阿新 • • 發佈:2021-01-25
技術標籤:controller 和dao
在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