求spring4 环境下 controller的单元测试的例子

humcomm 2014-04-29 09:58:12
spring 如何和spring自身的mock做单元测试,最好是结合了mockito
我的controller 是rest形式的

package joey.activity.web.rest;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@RequestMapping("")
public class UserActivityRest extends BaseRest {

@RequestMapping(value = "/userActivity/addUserActivity", method = RequestMethod.POST)
public void addUserActivity() {

}

@RequestMapping(value = "/userActivity/getUserActivity/{activityId}", method = RequestMethod.GET)
public void getUserActivity(@PathVariable("activityId") Long id) {
System.out.println("UserActivityRest.getUserActivity()");
}

}
...全文
427 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
loveunittesting 2016-01-07
  • 打赏
  • 举报
回复
引用 6 楼 yys79 的回复:
[quote=引用 5 楼 yinghan2005 的回复:] [quote=引用 3 楼 yys79 的回复:] 别用spring,切忌
为什么这么说呢?[/quote] spring有数据源死锁的问题。很多人遇到过,都给归于什么时间运行的比较常,或者并发比较大的原因,其实是spring事务管理的bug。要是你不用spring管理数据库事务的话,倒是可以用。[/quote] 看过淘宝技术总监写的一个高并发的书,确实感觉他们在使用spring,taobao都是分布式的服务,中间依赖注入,切面之类的确实是spring。但是数据库那里用不用这个真不知道。而且我觉得就算数据库方面的问题可以通过详细的事务配置避开,最大的短板还是存在,也就是OO方面较差,这个和EJB体系其实也一样,领域逻辑主要依赖“服务”来实现,无法这正是用面向对象的优势。
imjcoder 2015-12-28
  • 打赏
  • 举报
回复
引用 6 楼 yys79 的回复:
[quote=引用 5 楼 yinghan2005 的回复:] [quote=引用 3 楼 yys79 的回复:] 别用spring,切忌
为什么这么说呢?[/quote] spring有数据源死锁的问题。很多人遇到过,都给归于什么时间运行的比较常,或者并发比较大的原因,其实是spring事务管理的bug。要是你不用spring管理数据库事务的话,倒是可以用。[/quote] 连淘宝都用spring
loveunittesting 2014-05-04
  • 打赏
  • 举报
回复
引用 5 楼 yinghan2005 的回复:
[quote=引用 3 楼 yys79 的回复:] 别用spring,切忌
为什么这么说呢?[/quote] spring有数据源死锁的问题。很多人遇到过,都给归于什么时间运行的比较常,或者并发比较大的原因,其实是spring事务管理的bug。要是你不用spring管理数据库事务的话,倒是可以用。
humcomm 2014-05-01
  • 打赏
  • 举报
回复
引用 3 楼 yys79 的回复:
别用spring,切忌
为什么这么说呢?
tony4geek 2014-04-30
  • 打赏
  • 举报
回复
loveunittesting 2014-04-30
  • 打赏
  • 举报
回复
别用spring,切忌
别闹腰不好 2014-04-30
  • 打赏
  • 举报
回复
import org.junit.Assert; 02 import org.junit.BeforeClass; 03 import org.junit.Test; 04 import org.junit.runner.RunWith; 05 import org.springframework.beans.factory.annotation.Autowired; 06 import org.springframework.http.HttpMethod; 07 import org.springframework.mock.web.MockHttpServletRequest; 08 import org.springframework.mock.web.MockHttpServletResponse; 09 import org.springframework.test.context.ContextConfiguration; 10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 import org.springframework.web.method.HandlerMethod; 12 import org.springframework.web.servlet.ModelAndView; 13 import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; 14 15 @RunWith(SpringJUnit4ClassRunner.class) 16 @ContextConfiguration(locations = {"classpath:/WEB-INF/monitor-servlet.xml", "classpath:/spring/spring-core.xml"}) 17 public class JobControllerTest { 18 @Autowired 19 public RequestMappingHandlerAdapter handlerAdapter; 20 @Autowired 21 private JobController jobController; 22 23 private static MockHttpServletRequest request; 24 25 private static MockHttpServletResponse response; 26 27 @BeforeClass 28 public static void before() { 29 request = new MockHttpServletRequest(); 30 request.setCharacterEncoding("UTF-8"); 31 response = new MockHttpServletResponse(); 32 } 33 34 @Test 35 public void testList() { 36 request.setRequestURI("/job/list"); 37 request.setMethod(HttpMethod.POST.name()); 38 ModelAndView mv = null; 39 try { 40 mv = handlerAdapter.handle(request, response, new HandlerMethod(jobController, "list")); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 45 Assert.assertNotNull(mv); 46 Assert.assertEquals(response.getStatus(), 200); 47 Assert.assertEquals(mv.getViewName(), "/job/job_list"); 48 } 49 }

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧