Могу ли я узнать, что не так в приведенном ниже тестовом примере, написанном с использованием PowerMock?
Вот тестируемый метод:
public final Class ChecksConfig {
protected static Map<String, Object> getConfig() {
return getAppConfig()
}
private static Map<String, String> getAppConfig() {
File[] files = new File("/home/").listFiles();
if (null == files) {
return Collections.emptyMap();
}
return getConfigKeyMap(files); //Test does not reach this line as the IF condition is true.
// But this code need to be covered
}
}
Я полагаю, это потому, что file[]
выглядит как нуль и не издевается должным образом. Могу ли я узнать, что я делаю неправильно в приведенном ниже методе тестирования?
@RunWith(PowerMockRunner.class)
@PrepareForTest(ChecksConfig.class)
public class ChecksConfigTest {
@InjectMocks
ChecksConfig checksConfig;
@Rule
private TemporaryFolder folder = new TemporaryFolder();
@Test
public void testGetCheck() throws Exception {
PowerMockito.mockStatic(ChecksConfig.class);
File f = folder.newFile();
File[] files = { f };
File file = PowerMockito.mock(File.class);
PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(file);
PowerMockito.when(file.listFiles()).thenReturn(files);
PowerMockito.when(file.isFile()).thenReturn(true);
PowerMockito.when(checksConfig.getConfig()).thenReturn(map);
}