2017-01-09 136 views
0

我想寫一個測試控制器。下面是測試片段:春季啓動 - 控制器測試失敗,404代碼

@RunWith(SpringRunner.class) 
@WebMvcTest(WeatherStationController.class) 
@ContextConfiguration(classes = MockConfig.class) 
public class WeatherStationControllerTest { 

    @Autowired 
    private MockMvc mockMvc; 

    @Autowired 
    private IStationRepository stationRepository; 

    @Test 
    public void shouldReturnCorrectStation() throws Exception { 

     mockMvc.perform(get("/stations") 
       .accept(MediaType.APPLICATION_JSON)) 
       .andExpect(status().isOk()); 
    } 
} 

控制器代碼片段:

@RestController 
@RequestMapping(value = "stations") 
public class WeatherStationController { 

    @Autowired 
    private WeatherStationService weatherService; 

    @RequestMapping(method = RequestMethod.GET) 
    public List<WeatherStation> getAllWeatherStations() { 
     return weatherService.getAllStations(); 
    } 

    @RequestMapping(value = "/{id}", method = RequestMethod.GET) 
    public WeatherStation getWeatherStation(@PathVariable String id) { 
     return weatherService.getStation(id); 
    } 

MockConfig類:

@Configuration 
@ComponentScan(basePackages = "edu.lelyak.repository") 
public class MockConfig { 

    //**************************** MOCK BEANS ****************************** 

    @Bean 
    @Primary 
    public WeatherStationService weatherServiceMock() { 
     WeatherStationService mock = Mockito.mock(WeatherStationService.class); 
     return mock; 
    } 

這是錯誤的堆棧跟蹤:

java.lang.AssertionError: Status 
Expected :200 
Actual :404 

我能得到什麼在這裏是錯誤的。
如何解決控制器的測試問題?

回答

1

這是一個不同的方法來控制我的測試。

假設:類WeatherStationService@SpringBootApplication

接着,下面的測試類應該爲你工作:

@RunWith(SpringRunner.class) 
@SpringApplicationConfiguration(WeatherStationService.class) 
@WebIntegrationTest 
public class WeatherStationControllerTest { 

    @Autowired 
    private WebApplicationContext context; 

    MockMvc mockMvc; 

    @Before 
    public void setup() { 
     mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build(); 
    } 

    @Test 
    public void shouldReturnCorrectStation() throws Exception { 
     mockMvc.perform(get("/stations") 
       .accept(MediaType.APPLICATION_JSON)) 
     .andExpect(status().isOk(); 
    } 
} 

有了這個測試設置,您應該不再需要MockConfig類。

+0

我試過了沒有幫助。 –

+0

我希望這個建議能夠有所幫助,而且還爲時不晚。 – Nkokhelox

1

我不知道爲什麼你的測試不起作用。但是我得到了另一個適用於我的解決方案。

@SpringBootTest 
public class ControllerTest { 

    @Autowired 
    private MockMvc mockMvc; 

    @Before 
    public void setup() { 
     this.mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).build(); 
    } 

    @Test 
    public void shouldReturnCorrectStation() throws Exception { 
     mockMvc.perform(get("/stations") 
       .accept(MediaType.APPLICATION_JSON)) 
       .andExpect(status().isOk()); 
    } 
} 
+0

這裏是鏈接到完整的[控制器測試](https://github.com/nazar-art/geo-api-data/blob/master/src/test/java/edu/lelyak/controllers/WeatherStationControllerTest.java) –

+0

你如何看待全面的控制器測試? –

+0

@nazar_art我克隆了你的github項目,但它充滿了錯誤。不確定您的MockMvc是否正確。我會像我提供的那樣輕鬆地在您的測試課程中註冊一個控制器。 – Patrick

1

HTTP code 404,意味着沒有發現資源(在服務器上)爲您的要求,我認爲你的控制器是不可見的(讓我說是不是掃描)的彈簧啓動。

一個簡單的解決方案是在MockConfig類掃描父包,所以春季可以拿起所有的豆類,

@ComponentScan(basePackages = "edu.lelyak") // assuming that's the parent package in your project 

,如果你不喜歡這種方式,你可以在basePackages

添加控制器的包名
@ComponentScan(basePackages = {"edu.lelyak.controller","edu.lelyak.repository") 

BTW,您不必手動設置WeatherStationServiceMockConfig類呢,春天開機即可注入模擬爲你自動每個測試方法後重置它,你應該去克萊爾在您的測試類:

@MockBean 
private IStationRepository stationRepository; 

在另一方面,你應該在你的測試方法調用get("/stations")(因爲你沒有運行integration test)之前嘲笑weatherService.getAllStations(),所以你可以做:

List<WeatherStation> myList = ...; 
//Add element(s) to your list 
Mockito.when(stationService.getAllStations()).thenReturn(myList); 

你可以找到更多在: