简体   繁体   English

如何使用 EasyMock 在 Java 中测试受保护的最终方法?

[英]How to test protected final method in Java using EasyMock?

I want to write test case for below protected final method in java using EasyMock.我想使用 EasyMock 在 java 中为以下受保护的最终方法编写测试用例。 Is there any way to write test case of protected final method using EasyMock?有没有办法使用 EasyMock 编写受保护的最终方法的测试用例?

I tried to write TC using Reflection approach,but it didn't work.我尝试使用反射方法编写 TC,但没有成功。

Class < ? extends Entity > type;
private Filter filter;
private Input input;
private transient Service access;
private transient ConfigDao confdao;

protected final Limitation getBaseLimitation() {
    Validate.notNull(type);
    GroupClass Group = new GroupClass(GroupTypeClass.SELECTOR);
    if (A.class.isAssignableFrom(type)) {
        filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE); //A class has static final static String ATTRIBUTE_ACTIVE = "Active";
    }
    if (G.class.isAssignableFrom(type)) {
        filter = new Simple("position", Operator.EQUALS, Position.ACTIVE);
    }
    if (Boolean.TRUE.equals(confdao.getconfdao().getarea())) {
        if (U.class.isAssignableFrom(type)) {
            Validate.notNull(input, "switched on.");
            Object Inputobj = input.getInput();
            return access.getBaseLimitation(type, Inputobj, Group, filter);
        }
    }
    return access.getBaseLimitation(type, Group, filter);
}

public Simple(String path, Operator operator, Object value) {
    this(Path, operator, new Object[]{value});
}

Any help would be appreciated.任何帮助,将不胜感激。

If you are mocking getBaseLimitation() , no, EasyMock can't help you because final methods can't be overridden.如果你是 mocking getBaseLimitation() ,不,EasyMock 帮不了你,因为 final 方法不能被覆盖。 PowerMock could help you but I would personally just drop the final . PowerMock 可以帮助你,但我个人会放弃final

If you are testing it, you can from a class in the same package.如果您正在测试它,您可以从 class 中的同一个 package 中。 But EasyMock is not needed to do so.但 EasyMock 不需要这样做。

Then, protected final is not something useful.那么, protected final就没什么用了。 Why being protected if you don't want to be overloaded?如果您不想超载,为什么要受到保护? You better be package scope or private.你最好是 package scope 或私人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM