リダイレクトして遷移した画面のテストケースを作成しています。
(下記サンプルソースのTest2Controle.test2です)
リダイレクト前に、addFlashAttributeにMapをセットしているのですが、
この部分をどう書けばよいのか悩んでいます。
1.直書き。
Map<String, Object> map;
map.put("code", "01");
map.put("name", "test");
2.DBUnitを使う。
DatabaseConnection conn = new DatabaseConnection(getDataSource().getConnection());
String query = "Select * From tttest where code = '01'";
ITable queryTableActual = conn.createQueryTable("ExpectedData", query);
ITableMetaData metaData = queryTableActual.getTableMetaData();
columns = metaData.getColumns();
カラム名と値を取得して作る
今の知識でできるのは上記2つの方法ですが、もっと良い方法はありませんか?
それとも、そもそものやり方が違いますか?
ご存知の方は、教えて頂けると助かります。
java
1/* 2 * Test1Controle 3 */ 4@RequestMapping(value = "/test1", params = "detail", method = RequestMethod.POST) 5public String detai(Test1Form test1Form, Locale locale, Model model 6 , SessionStatus ss, RedirectAttributes redirectAttr, HttpServletRequest request) 7 throws ServletException { 8 9 // 詳細画面に表示する値を取得 10 Map<String, Object> map = 次画面で使用するデータ; 11 12 // 引き渡すForm(情報)は呼び出す画面のForm。 13 this.test2Form.setMap(map); 14 15 // Redirect Attributeに登録。 16 redirectAttr.addFlashAttribute("test2Form", this.test2Form); 17 redirectAttr.addFlashAttribute("serverTime", CommonUtils.formattedDate(locale) ); 18 19 // 詳細画面へ 20 return "redirect:/test2"; 21} 22 23/* 24 * Test2Controle 25 */ 26@RequestMapping(value = "/test2", method = RequestMethod.GET) 27public String test2(Test2Form test2Form 28 , Locale locale, Model model, HttpServletRequest request) { 29 30 // レコードマップを取得 31 Map<String, Object> map = test2Form.getMap(); 32 33 // 値設定 34 test2Form.setCode( map.get("code").toString().trim() ); 35 test2Form.setName( map.get("name").toString().trim() ); 36 37 model.addAttribute("test2Form", test2Form); 38 model.addAttribute("serverTime", CommonUtils.formattedDate(locale) ); 39 return "test2"; 40} 41 42@Test 43public void testTest2() throws Exception { 44 45 // MAP生成 46 Map<String, Object> map = ??? 47 Test2Form form = new Test2Form(); 48 form.setMap(map); 49 50 MvcResult mvcResult = this.mockMvc.perform( 51 get("/test2").flashAttr("test2Form", form).session(this.mockHttpSession)) 52 .andExpect(status().isOk()) 53 .andExpect(view().name("test2")) 54 .andExpect(model().attributeExists("test2Form")) 55 .andExpect(model().attributeExists("serverTime")) 56 .andExpect(model().hasNoErrors()).andReturn(); 57}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/09 07:11