[英]How to use EasyMock to mock a protected method?
public class A{
protected Integer methodA(String a){
//some code is included;
return new Integer(1);
}
}
public class B extends B{
String b = "AnyThing";
methodA(b);
//there are also other methods that will be tested
}
The following is partial test codes 以下是部分测试代码
B classUnderTest = createMockBuild(B.class).addMockedMethod(B.class.getDeclaredMethod(methodA(), String.class)).createMock;
expect(classUnderTest.methodA(anyObject(String.class))).andReturn(new Integer(1));
The second line of the second piece of code even cannot get through compile. 第二段代码的第二行甚至无法通过编译。 Where is wrong? 哪里错了?
Jane, regarding your main question, the line 简,关于您的主要问题,
expect(classUnderTest.methodA(anyObject(String.class))).andReturn(new Integer(1));
will compile successfully if the @Test
class is in the same package
as your subject under test - in this case both classes A
and B
(if class B extends A
). 如果@Test
类与要测试的主题位于同一个package
,则将成功编译-在这种情况下,类A
和B
(如果class B extends A
)。 Protected instance methods are only visible to classes in the same package. 受保护的实例方法仅对同一包中的类可见。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.