jmockit mock 类的static 属性

时间:2023-06-09 08:05:55
final Object[] originValue = new Object[1];
try{

  new Expectations(XXStatic.class){

    {

    

    originValue[0] =
Deencapsulation.getField(XXStatic.class, "fieldName");//保存更改前的value

    Deencapsulation.setField(XXStatic.class,"fieldName","mockValue");

    }

  }

}finally{
//恢复现场 否则会影响其他测试用例 即依赖真实 XXStatic.fieldName 的用例们

    Deencapsulation.setField(XXStatic.class,"fieldName",originValue[0]);

}

重点 Deencapsulation