【问题标题】:How to write expect statement for new keyword using EasyMock?如何使用 EasyMock 为新关键字编写期望语句?
【发布时间】:2019-11-18 11:46:31
【问题描述】:

我想只使用 EasyMock 为 filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE); 行编写期望语句。

//SingleLimit.java
private Limitation filter;

protected final Limitation getBaseLimitation() {
    Validate.notNull(type);
    GroupClass Group = new GroupClass(GroupTypeClass.SELECTOR);
    if (Activatable.class.isAssignableFrom(typeListed)) {
        if (A.class.isAssignableFrom(type)) {
        //expect statement for below line
        filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE); 
        }
     }
    }


   //Simple.java
   public class Simple implements Serializable, Limitation
   {
     public Simple(String path, Operator operator, Object value) {
    this(path, operator, new Object[]{value});
   }
    }

任何帮助将不胜感激

【问题讨论】:

    标签: new-operator easymock


    【解决方案1】:

    我将创建一个创建简单对象的新类。

    public class SimpleFactory {
        public Simple createSimple(....) {
            return new Simple(....);
        }
    }
    

    将这个工厂注入你的类或将其作为参数传递

    public YourClazz(SimpleFactory simpleFactory) {
    
        //constructor 
        this.simpleFactory = simpleFactory;
    
        protected final Limitation getBaseLimitation() {
            ///...
            //filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
            //change this to
            filter = simpleFactory.createSimple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
        }
    
        //or pass as parameter
        protected final Limitation getBaseLimitation(SimpleFactory simpleFacotry) {
            ///...
            //filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
            //change this to
            filter = simpleFactory.createSimple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
        }    
    }
    

    现在在您的测试代码中创建一个 simpleFactory 的模拟。

    //2019/11/16 编辑另一种解决方案

    如果你不能创建其他类,在你的类中创建新方法,然后用它来创建新的简单对象:

        public YourClazz {
    
            protected Simple createSimple(....) {
                return new Simple(....);
            }
    
            protected final Limitation getBaseLimitation() {
                //...
                //filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
                //change this to
                filter = createSimple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
        //...
            }    
    }
    

    现在为了测试,您可以创建类的部分模拟并模拟 createSimple 方法:

    YourClazz mock = partialMockBuilder(YourClazz.class)
          .addMockedMethod("createSimple").createMock();
    
    
    Simple expectedSimple = new Simple(....);      
    
    expect(mock.createSimple(.....).andReturn(expectedSimple);
    replay(mock);
    

    【讨论】:

    • 感谢您分享您的想法。但是,我无法按照您的建议创建新课程。这是一个限制。
    • 非常感谢,我在代码 sn-p 中提到了“new Simple()”的行为。它是“Simple.java”类的构造函数。
    • 您不能使用 EasyMock 模拟构造函数。解决问题的唯一方法是重构代码以使用某种方法来创建简单对象并在测试中模拟此方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多