如何仅使用EasyMock模拟静态void方法?

问题描述 投票:0回答:1

我想使用EasyMock在我的TestB.java类中模拟A.notNull(obj)方法。自一个星期以来,我一直在努力模拟这种方法。

//A.class
public class A
{
    public static void notNull(Object o)
    {
       notNull(o,"object is null");
    }
    public static void notNull(Object o, String s)
    { 
       if (o==null)
       {
           throw new IllegalArgumentException(s);   
       }
    }
 }

 //B.class
 Class<? extends E> obj;

 protected final Simple limit()
 {
     A.notNull(obj); //I want to mock this line in my TestB.java class using EasyMock framework
 }

任何帮助,将不胜感激。

java methods static void easymock
1个回答
0
投票

实际上,EasyMock不支持模拟静态方法。您需要在其顶部添加PowerMock来这样做。

但是,在您的情况下,我确实不会嘲笑notNull。它没有做任何需要嘲笑的事情。您甚至不需要此方法,因为Objects.requireNotNull执行相同的操作。

© www.soinside.com 2019 - 2024. All rights reserved.