jmockit mock 方法内创建对象的方法调用

在测试中,经常会遇到无法直接mock方法内部创建的对象。JMockit提供了一个解决方案,允许我们mock整个类的实例,无论何时在执行过程中创建该类的对象,它都会自动被mock,从而实现更灵活的测试覆盖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


    测试的过程中,经常会出现的情况是有些需要mock的对象是在方法调用过程中创建的;我们不能通过创建mock对象然后设置到被测对象中的方式来实现;
    天无绝人之路,jmockit给我们提供了可以mock一个类的所有被创建的对象,就是说不管你是在一次执行过成中的任何位置使用该类,都可以自动的被mock;


    import static  mockit.Deencapsulation.*;
    @Test
    public void testMdm(@Mocked final HttpProtobufComponentExecutor mybatisComponentExecutor) throws EngineException {
        /*测试调用参数*/
        Map params = new HashMap();
        params.put("acctId" , 10);

        /*被mock对象方法的返回值*/
        final Map mockResult = new HashMap();
        mockResult.put("acctId",20);
        mockResult.put("acctName","test");
        mockResult.put("acctStatus",10);

        /*
         mock逻辑:
           1.mock调用过程中遇到的所有HttpProtobufComponentExecutor类对象的私有方法invokeTainluRpcService
        */
        new NonStrictExpectations(HttpProtobufComponentExecutor.class){
            {
               invoke(mybatisComponentExecutor, "invokeTainluRpcService", withAny(ConsumerConfig.class),withAny(Object.class),anyString);     result = mockResult;

            }
        };
        /*
        callComponentPB方法调用中会创建HttpProtobufComponentExecutor对象,并且调用invokeTainluRpcService方法,该方法由上面的mock逻辑模拟
	*/  
	Object result = this.callComponentPB("mdmQueryAcct", params);        
	Assert.assertNotNull(result);        
	System.out.println(result);    
    }



      注意:
      Deencapsulation.invoke方法中不能使用any这样的参数,否则必须使用正确类型的参数;
例如下面的调用会报异常

    invoke(mybatisComponentExecutor, "invokeTainluRpcService", withAny(ConsumerConfig.class),any,anyString);     result = mockResult;
				
    java.lang.IllegalArgumentException: Invalid null value passed as argument 1
	at com.baidu.rigel.bpip.ka.AcsTest$1.<init>(AcsTest.java:137)
	at com.baidu.rigel.bpip.ka.AcsTest.testMdm(AcsTest.java:135)




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值