JAVA初心者です。
SpringMVC JUnit Mockito を使用して、単体テストの作成をしています。
JSPで指定したファイルのアップロードを行う機能のテストケースを作成したいのですが、以下のように書いた所、「java.lang.AssertionError: Status expected:<200> but was:<400>」エラーになります。
「this.mockMvc.perform(fileUpload("/testview").file(file)...」
の書き方が悪いのだとは思うのですが、どこが問題なのか、どう書けば良いのか分かりません。
Postの場合だと、
「this.mockMvc.perform(post("/testview").session(this.mockHttpSession).param("search", "検索"))」
で期待した結果が帰るのですが。
ご教授頂けると助かります。
<!-- testView.jsp --> <form action="testview" method="POST" enctype="multipart/form-data"> <label for="file" > ファイル選択 <input type="file" name="file" id="file" onchange="uploadFileName.value = this.value;" /> </label> <input type="text" id="uploadFileName" style="width: 500px; margin-right: 10px;"/> <input type="submit" name="upload" value="送信"/> </form> // コントロールクラス @RequestMapping(value = "/testview", params = "upload", method = RequestMethod.POST) public String upload(@RequestParam("file") MultipartFile file, Locale locale, Model model, HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException { // 処理... // 正常登録の場合 model.addAttribute("serverTime", CommonUtils.formattedDate(locale)); return "testView"; } // テストケース @Test public void testUpload1() throws Exception { byte[] fileImage = null; File upfile = new File("src/test/java/" + getClass().getPackage().getName().replace(".", "/") + "/uploadFile.csv"); Path path = Paths.get(upfile.getAbsolutePath()); if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { fileImage = Files.readAllBytes(path); } MockMultipartFile file = new MockMultipartFile("uploadFile.csv", "testfile.csv", null, fileImage); this.mockMvc.perform( fileUpload("/testview").file(file) .param("upload", "Upload(CSV)")) .andExpect(status().isOk()) .andExpect(view().name("testView")) // 遷移先View名の判定。 .andExpect(model().attributeExists("serverTime")) // Modelにサーバ処理時間がセットされているか?判定。 .andExpect(model().hasNoErrors()); // 呼び出し結果にエラーを保持していないか?判定。 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。