2016-06-12 6 views
1

私はコントローラのテストケースを書くつもりです。これらのテストを完全な機能テストとして使用したいので私のサービスを模擬したくありません。MockMvcが404の状態を返す

私はこのコントローラをテストしようとしています:ここで

@Controller 
public class PlanController { 

    @Autowired 
    private PlanService planService; 

    @RequestMapping(
     value = "/api/plans/{planId}", 
     method = RequestMethod.GET, 
     produces = MediaType.APPLICATION_JSON_VALUE) 
    @ResponseBody 
    @Nonnull 
    @JsonView(Plan.SimpleView.class) 
    public Plan getPlan(@RequestParam int orgId, @PathVariable int planId) { 
     Plan plan = planService.getPlan(orgId, planId); 
     return plan; 
    } 
} 

をしては、私が書かれているテストケースである:

package com.videology.skunkworks.audiencediscovery.controller; 

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 

import org.eclipse.jetty.webapp.WebAppContext; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.MockitoAnnotations; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.MediaType; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.web.WebAppConfiguration; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 
import org.springframework.web.context.WebApplicationContext; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {WebAppContext.class}) 
@WebAppConfiguration 
@EnableWebMvc 
public class PlanControllerTest { 

    @Autowired 
    private WebApplicationContext wac; 

    private MockMvc mockMvc; 

    @Before 
    public void setup() { 
     MockitoAnnotations.initMocks(this); 
     this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build(); 
    } 

    @Test 
    public void testGetPlan() throws Exception { 
     mockMvc.perform(get("/api/plans/1/?orgId=1").accept(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()); 
    } 
} 

返されるステータスが()404でない200であるため、このテストケースは失敗していますerrorMessageがnullのため、なぜ404を返すのか分かりません。

私は多くの似たような疑問を抱いていますが、それらのどれも私には役に立たなかった。

+0

'get("/api/plans/1 /?orgId = 1 ")'このURLは間違っています。代わりに 'get("/api/plans/1?orgId = 1 ")'を試してください。 –

+0

私は確かに、それはここでは関係ありません。 私はまだあなたのやり方を試して、同じエラーに直面しています。 –

+0

これを解決しましたか?私は問題に遭遇した。ちょうど新しいコントローラ、MockMvcテストを作成しました。テストアプリのコンテキストにコントローラが表示されないようです: - /。 –

答えて

0

エラーが発生しました。 これを修正するために、WebConfigファイルをcontextConfigurationで提供する必要がありました。ここに私が追加した行があります:

@ContextConfiguration(classes = {WebConfig.class}) 
関連する問題