【发布时间】:2020-03-10 06:59:11
【问题描述】:
我想使用 EasyMock 在 java 中为以下 protected final method 编写测试用例。有没有办法使用 EasyMock 编写受保护的最终方法的测试用例?
我尝试用反射的方式写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});
}
我们将不胜感激。
【问题讨论】:
-
我个人不建议直接测试非 api(非公开)方法。而是测试使用此方法的公共方法。受保护的方法是实现细节。可以测试非公共方法,但我再次建议不要这样做。
-
我们可以使用反射为受保护的方法编写测试用例,但不确定受保护的最终方法。
-
如果您知道如何为
protected方法编写 TC,如果您不确定是否可行,请尝试使用protected final。如果您对此有任何问题,请回来并描述这些问题。 -
我已经为受保护的最终版本尝试了相同的方法,但没有成功。
-
怎么没用?请编辑您的问题。
标签: java testng final protected easymock