2016-09-15 33 views
0

残りのAPIをテストするためのSpring Junitクラスを1つ作成しています。しかし私がそれを呼び出すと、私は404を返し、テストケースは失敗しました。 JUnitテストクラスは次のとおりです。Spring Junitテストで404エラーコードが返される

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration(locations={"classpath:config/spring-commonConfig.xml"}) 
public class SampleControllerTests { 

public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(
     MediaType.APPLICATION_JSON.getType(), 
     MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); 

private MockMvc mockMvc; 
@Autowired 
private WebApplicationContext webApplicationContext; 

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

@Test 
public void testSampleWebService() throws Exception { 
    mockMvc.perform(post("/sample/{userJson}", "{\"id\":\"102312121\",\"text\":\"Hi user\",\"key\":\"FIRST\"}")) 
    .andExpect(status().isOk()) 
    .andExpect(jsonPath("$result", is("Hello "))) 
    .andExpect(jsonPath("$answerKey", is(""))); 
} 
} 

RestControllerクラスは次のとおりです。

@RestController 
public class SampleController { 

private static final Logger logger = LoggerFactory.getLogger(SampleController.class); 
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create(); 

@Autowired private SampleService sService; 


@RequestMapping(value = "${URL.SAMPLE}", method = RequestMethod.POST) 
@ResponseBody 
public String sampleWebService(@RequestBody String userJson){ 
    String output=""; 
    try{ 
     output = sService.processMessage(userJson); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
    return output; 
} 
} 

私はプロパティファイルからURL文字列をロードしています。このようにして、私はコントローラクラスのURLをハードコードしているのではなく、動的にマッピングしています。そのため、以下で述べるクラスを介してプロパティファイルをロードしています。

"URL.SAMPLE = /サンプル/ {userJson}"

URLが定義されているプロパティファイル読み込みクラス:

@Configuration 
@PropertySources(value = { 
    @PropertySource("classpath:/i18n/urlConfig.properties"), 
    @PropertySource("classpath:/i18n/responseConfig.properties") 
}) 
public class ExternalizedConfig { 

@Bean 
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 
} 

をエラーコード404の手段、それサーバーに接続していますが、要求したソースが取得されていません。 誰かが問題の正確さを教えていただけますか?

おかげで、あなたは現在@RequestBodyを使用して要求本体から解析されたJSON入力を持ってしようとしている アトゥール

答えて

0

ご回答ありがとうございます。

あなたが示唆した内容を変更しましたが、エラーは依然として残っています。

もう一度エラーで検索して解決策を見つけました。

私はSpring XMLファイルで変更を加えました。 Like:

mvcのxmlファイルに名前空間を追加します。

xmlns:mvc="http://www.springframework.org/schema/mvc" 

http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 

    <context:component-scan base-package="XXX" /> 
<mvc:annotation-driven /> 

これが機能しています。

1

。ただし、リクエスト本文としてコンテンツを送信しているわけではありません。

URL内にリクエスト本文をエンコードしようとしていますが、そのようには機能しません。

修正するには、次の手順を実行する必要があります。要求パス(ない/sample/{userJson}

  • として

    1. 利用/sample要求のとしてテストJSON入力を提供します。

    次のようにすることができます。

    @Test 
    public void testSampleWebService() throws Exception { 
        String requestBody = "{\"id\":\"102312121\",\"text\":\"Hi user\",\"key\":\"FIRST\"}"; 
    
        mockMvc.perform(post("/sample").content(requestBody)) 
         .andExpect(status().isOk()) 
         .andExpect(jsonPath("$result", is("Hello "))) 
         .andExpect(jsonPath("$answerKey", is(""))); 
    } 
    
  • 関連する問題