我試圖使用@RunWith
& @SpringBootTest
來測試我的控制器。春季啓動1.5.2控制器層測試
控制器
@RestController
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String get(HttpServletResponse response) throws IOException {
return "Hello World";
}
}
測試類
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class SampleTestNGApplicationTests{
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testHome() throws Exception {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/home", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World");
}
}
相關性進行測試
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
現在@RunWith
& @SpringBootTest
註釋沒有發現,我失去了它的任何圖書館嗎?我知道Spring Boot 1.5.2的變化與Spring Boot 1.4.2相比有很大的變化。
更新
上面的問題,現在解決了,其實我創造了新的模塊,用於測試和控制器不同的充模塊中。我在測試模塊的main-> src-> java下編寫測試代碼,並且我在依賴關係中標記了spring-boot-starter-test
依賴範圍到test
,所以刪除了<scope>test</scope>
,現在我可以得到@RunWith
& @SpringBootTest
註釋。
現在我得到錯誤@ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)
錯誤日誌
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches:
-----------------
DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)
DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:
- @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)
罐子損壞,錯了進口。很難說。 –