日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

spring mvc+junit

發布時間:2024/4/13 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring mvc+junit 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

spring mvc的簡單單元測試,說白了就是測試spring mvc的controller.

先參考兩篇帖子:

1.http://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles

2.http://spring.io/blog/2012/11/07/spring-framework-3-2-rc1-new-testing-features


實踐:

UserController.java

package com.test.web; import java.io.File; import java.io.IOException; import java.util.List;import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile;import com.test.domain.User; import com.test.service.UserService; @Controller @RequestMapping("/user") public class UserController{private static final String FILES_DIR="e:/";@Resourceprivate UserService userService;@RequestMapping("/save")public String save(User user) {userService.save(user);return "result";}@RequestMapping("/getUser")public @ResponseBody User getUser(HttpServletRequest request,@RequestBody User user) {System.out.println("AAAAAAA::"+user);request.setAttribute("data1",user);return user;}@RequestMapping("/getUsers")public @ResponseBody List<User> getUsers(HttpServletRequest request,@RequestBody List<User> users) {System.out.println(users.getClass().getName());for (User user : users) {System.out.println("BBBBBBBB::"+user);}request.setAttribute("data2", users);return users;}@RequestMapping(value = "/upload", method = RequestMethod.POST)public String add(HttpServletRequest request,@RequestParam("file") MultipartFile file) {//System.out.println("real path::"+request.getServletContext().getRealPath("/"));String filename=file.getOriginalFilename();File tmpFile=new File(FILES_DIR+filename);if(filename!=null&&!file.isEmpty()){try {FileCopyUtils.copy(file.getBytes(), tmpFile);System.out.println("上傳成功");} catch (IOException e) {e.printStackTrace();}}return "result";}}
2.UserControllerTest.java

package com.test.web; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockMultipartFile; 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 com.test.config.AppConfig; import com.test.domain.User; import com.test.util.JsonUtil; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes={ AppConfig.class}) public class UserControllerTest {@Autowiredprivate WebApplicationContext webApplicationContext;private MockMvc mockMvc;@Beforepublic void init(){mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();}@Testpublic void printBeans(){String[] beans=webApplicationContext.getBeanDefinitionNames();for (String bean : beans) {System.out.println(bean);}}/*測試以普通請求參數發送的請求*/@Testpublic void testSave() throws Exception {mockMvc.perform(post("/user/save").param("id", "123").param("username", "you")).andExpect(status().isOk()).andExpect(view().name("result"));}/*測試將數據以JSON格式寫入請求體發送的請求*/@Testpublic void testGet() throws Exception {mockMvc.perform(post("/user/getUser").contentType(JsonUtil.APPLICATION_JSON_UTF8).content(JsonUtil.convertObjectToJsonBytes(new User(22,"werwr")))).andExpect(status().isOk());}/*測試將數據以JSON格式寫入請求體發送的請求*/@Testpublic void testGetAll() throws IOException, Exception {List<User> list=new ArrayList<User>();list.add(new User(23,"你愛我"));list.add(new User(25,"我不愛你"));mockMvc.perform(post("/user/getUsers").contentType(JsonUtil.APPLICATION_JSON_UTF8).content(JsonUtil.convertObjectToJsonBytes(list))).andExpect(status().isOk());}/*測試文件上傳發送的請求*/@Testpublic void testUpload() throws Exception{MockMultipartFile file = new MockMultipartFile("file", "orig.txt", null, "bar".getBytes());mockMvc.perform(fileUpload("/user/upload").file(file)).andExpect(status().isOk());} }
以上就貼出了spring mvc的單元測試最核心內容和舉例的最核心代碼.


完整源代碼:http://download.csdn.net/detail/xiejx618/7037623



順便貼一下spring junit(非controller簡單bean)

package com.test.service; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.test.domain.User; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class UserServiceTest {@Configuration@ComponentScan(basePackages={"com.test.service"})static class ContextConfiguration {}@Resourceprivate UserService userService;@Testpublic void testSave(){userService.save(new User(123,"xxx"));}}

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

轉載于:https://my.oschina.net/mayude/blog/503638

總結

以上是生活随笔為你收集整理的spring mvc+junit的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。