(三)JMockit API:@Mocked -基础篇

@Mocked可以修饰一个类,接口等。

  1. 当@Mocked修饰一个类
import mockit.Mocked;
import org.junit.Assert;
import org.junit.Test;

import java.util.Locale;

**//@Mocked 注释用途-01**
public class MockedClassTest {
    //加上了JMockit的API @Mocked,JMockit会帮我们实例化这个对象,不用担心它为null
    @Mocked
    Locale locale;
    @Test
    public void testMockedClass(){
        //静态方法不起作用了,返回了null
        Assert.assertTrue(locale.getDefault() == null);

        //非静态方法(返回类型为string)也不起作用了,返回了null
        Assert.assertTrue(locale.getCountry() == null);

        //自己new一个,也同样如此,方法都被mock了
        Locale chinaLocale = new Locale("zh","CN");
        Assert.assertTrue(chinaLocale.getCountry() == null);
    }
}
  1. 当@Mocked修饰一个接口/抽象类
**//@Mocked 注释用途-02**
public class MockedInterFaceTest {
    //加上了JMockit的API @Mocked,JMockit会帮我们实例化这个对象,尽管这个对象的类型是一个接口,不用担心它为null
    @Mocked
    HTTPSession session;

    //当@mocked 作用于interface
    @Test
    public void testMockedInterface(){
        // (返回类型为String)也不起作用了,返回了null
        Assert.assertTrue(session.getId() == null);

        // (返回类型为原始类型)也不起作用了,返回了0
        Assert.assertTrue(session.getCreationTime() == 0L);

        // (返回类型为原非始类型,非String,返回的对象不为空,这个对象也是JMockit帮你实例化的,
        // 同样这个实例化的对象也是一个Mocked对象)
        Assert.assertTrue(session.getServletContext() != null);

        // Mocked对象返回的Mocked对象,(返回类型为String)的方法也不起作用了,返回了null
        Assert.assertTrue(session.getServletContext().getContextPath() == null);
    }
}

3. @Mocked功能总结
通过上述例子,可以看出:@Mocked修饰的类/接口,是告诉JMockit,帮我生成一个Mocked对象,这个对象方法(包含静态方法)返回默认值。
即如果返回类型为原始类型(short,int,float,double,long)就返回0,如果返回类型为String就返回null,如果返回类型是其它引用类型,则返回这个引用类型的Mocked对象(这一点,是个递归的定义,需要好好理解一下)。

  1. 什么测试场景,我们要使用@Mocked

当我们的测试程序依赖某个接口时,用@Mocked非常适合了。只需要@Mocked一个注解,JMockit就能帮我们生成这个接口的实例。
比如在分布式系统中,我们的测试程序依赖某个接口的实例是在远程服务器端时,我们在本地构建是非常困难的,此时就交给@Mocked,就太轻松啦!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值