在Mockito/PowerMockito中有没有一种方法可以模拟超类的某些方法。
下面是一个场景,它进行了多级继承。
public class Network {
public String getNetwork() {
return "Technology";
}
}
这里有另一个类 Technology
,它扩展了 Network
。
public class Technology extends Network {
public Type getTechnology() {
String network = getNetwork();
if(network == null){
return null;
}
return network;
}
}
另一个继承自Technology
的类是Service
public class BaseService extends Technology {
public void getServices() {
Type type = getTechnology();
if(type == null){
throw new Exception('Type not found');
}
}
}
我希望为BaseService类的方法getServices
编写一个测试用例,当技术类Type
为空时,它应该抛出异常。
我尝试了几个步骤,但都没有成功。
样例测试用例已编写完成。
@Test
public void test_get_services_should_throw__exception_when_type_is_null() throws Exception{
//Arrange
expectedException.expect(Exception.class);
baseService = Mockito.mock(BaseService.class, Mockito.CALLS_REAL_METHODS);
when(baseService.getTechnology()).thenReturn(null);
//Act
baseService.getServices();
}
上面的代码片段只是一个示例,可能包含错误。如果有,请忽略。
请帮我解决这个问题。
提前致谢。