残りの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入力を持ってしようとしている アトゥール