Is there anyway in Mockito/PowerMockito to mock some methods of super class.
Below is a scenario, it does a multilevel inheritance.
public class Network {
public String getNetwork() {
return "Technology";
}
}
Here is another class Technology
that extends Network
public class Technology extends Network {
public Type getTechnology() {
String network = getNetwork();
if(network == null){
return null;
}
return network;
}
}
Another class Service
that extends Technology
public class BaseService extends Technology {
public void getServices() {
Type type = getTechnology();
if(type == null){
throw new Exception('Type not found');
}
}
}
I want to write a test case for BaseService class method getServices
, so that it should throw an exception when the technology class Type
is null.
I have tried with a few steps but couldn't help.
Sample test case written
@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();
}
The code snippet above is just a sample and may contain errors. Please ignore if any.
Please help me to resolve this issue.
Thanks in advance.