Jmockit入门

本文主要参考jmockit的官文,并不是全部的翻译,只是点出了一些重要理解的部分,其中参杂着一些个人的看法,实例也有来自官文的,以及实际的项目


1、项目中添加jmockit的使用,jmockit的jar包需要先于junit的jar引入,junit的版本也最好在4.5以上。特别说明,如果JDK使用1.5的话,需要特别配置,具体可以参见官网,http://code.google.com/p/jmockit/。

2、说到使用jmockit这个工具,首先当然知道目的,Developer需要写单测。程序员进行自测的方法分为单元测试和集成测试,这是个范畴的界定,简单点理解,单元测试是为了测试一段独立的逻辑代码,而集成测试是被测的逻辑,还有其他的依赖。而“mock”(adj.仿制的;模拟的;)的由来,正是需要将被测逻辑的依赖给消除,准确来说,不应该用消除这个字眼,虽然这是目的,应该是将这个“依赖”,可以随着自己的意愿更改。任何被mock的对象,都能按照自己的意愿来运行,使得test可以通过。

3、在“mock界”中,其实一些用的比较多的工具,EsayMock,Jmock,Mockito,Unitils Mock是比较有名的4种,后两者在笔者所在的项目中都有用到过。这些工具能进行mock的方式,是基于java.lang.reflect.Proxy,是在运行时创建一个java接口的实现,或者通过CGLIB-based为实体类创建一个代理,其中EsayMock和Jmock没有明确的验证mock的对象是否运行过;而Mockito和Unitils Mock是可以在单测运行之后,验证mock的对象,是否正的被调用了。

4、这些传统的mock工具,其不足之处在于:(1)实现mock的类必须只实现一个单独的接口,或者不能被声明为final,需要被mock的方法也不能声明为final;(2)实现类不能为static,方法也不行;(3)所有可以被mock的方法必须是public的,至少不能是私有的,但是实践告诉我,mock私有方法是有好处的,而且在有的时候,是很必要的;(4)被测类需要提供getter和setter,或者是constructor,用以加载被mock的依赖。而jmockit都能已经fix了,这也是jmockit和传统mock工具的不同之处,详细的区别,可以参考官网:http://jmockit.googlecode.com/svn/trunk/www/about.html。


写的略烂,我去~~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMockit is a Java library that provides support for mocking and testing. The @Qualifier annotation is used in JMockit to identify a specific instance of a bean to be used in a test. In Spring, the @Qualifier annotation is used in a similar way to identify a specific bean to be injected into a component. However, in JMockit, the @Qualifier annotation is used in conjunction with other annotations to specify which instance of a mock or spy object to use in a test. For example, consider a scenario where we have two implementations of a service interface and we want to mock one of them for testing. We can use the @Qualifier annotation to identify the bean to be mocked and the @Mocked annotation to create a mock object of that bean. ``` public interface MyService { String getName(); } @Service("fooService") public class FooService implements MyService { @Override public String getName() { return "Foo"; } } @Service("barService") public class BarService implements MyService { @Override public String getName() { return "Bar"; } } public class MyServiceTest { @Test public void testGetName(@Mocked @Qualifier("fooService") MyService fooService, @Mocked @Qualifier("barService") MyService barService) { new Expectations() {{ fooService.getName(); result = "Mocked Foo"; barService.getName(); result = "Mocked Bar"; }}; // Use the mocked instances of fooService and barService in the test // ... } } ``` In the above example, we have two implementations of the MyService interface, FooService and BarService, and we want to mock FooService for testing. We use the @Qualifier("fooService") annotation to identify the bean to be mocked and the @Mocked annotation to create a mock object of that bean. We also create a mock object of the BarService bean using the @Mocked and @Qualifier("barService") annotations. We can then use these mocked instances of the beans in our test.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值