2017-01-09 5 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が正しいかどうかは不明です。簡単な方法でテストクラスに1台のコントローラを登録したいと思います。 – 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") 

ところで、あなたは手動でWeatherStationServiceクラスMockConfigで設定する必要はありません、春ブーツがあなたのためにモックを注入し、自動的に各テストメソッドの後にそれをリセットすることができ、あなただけ解除すべきですあなたのテストクラスでそれをクレア:一方

@MockBean 
private IStationRepository stationRepository; 

(あなたがintegration testを実行していないよう)あなたが行うことができますので、あなたは、あなたの試験方法でget("/stations")を呼び出す前にweatherService.getAllStations()を模擬する必要があります

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

関連する問題