2017-05-05 42 views
1

我試圖使用@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) 
+0

罐子損壞,錯了進口。很難說。 –

回答

1

經過大量的時間支出就可以了,我在@SpringBootTest添加webEnvironment,這是爲我工作。

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
0
@SpringBootTest(classes = Application.class) 

將加載整個應用程序上下文。我想這不是必要的,如果你只想測試你的控制器層。你可以做這樣的事情

@RunWith(SpringRunner.class) 
@WebMvcTest(controllers = HomeController.class) 
public class HomeControllerTest { 

    @Autowired 
    private MockMvc mvc; 

    @MockBean 
    private SomeService someservice // Mock any other dependencies which you have in your controller. 

    @Test 
    public void someTest() throws Exception { 

    doAnswer(invocation -> { 
     // return some predefined data which would be returned from your service layer 
    }).when(someservice).someMethod(); 

    mvc.perform(MockMvcRequestBuilders.get("/home").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) 
     .andExpect(status().isOk()) 
     .andExpect(content().string("expectedString")); 
    } 

} 
+0

我刪除SpringBootTest並添加WebMvcTest,但其獲得的異常「無法加載的ApplicationContext」 –

+0

刪除@WebAppConfiguration,也不要使用RestTemplate(這需要整個應用程序上下文來進行設置)使用Mockmvc在我給的例子表明 – pvpkiran