powermock 1.5.1 Files, , / jdk1.7, . javassist , (3.18.0-GA),
"", . StringUtils.chop( "string" ); (commons-lang3), , mock.
, , Files, StringUtils.
, , , @PrepareForTest PowerMockito.mockStatic() .
. -, - .
. : , . PowerMock out (1.5.3), javassist (3.18.1-GA), , .
Files , @PrepareForTest, Files , . . , - Files.
:
public class MyTestClass {
public void justToTestMocking(Path path) throws IOException {
if (!Files.exists(path)) {
throw new IllegalArgumentException("I know there is a deleteIfExists() but I am just testing mocking");
}
Files.delete(path);
}
}
:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Files.class, MyTestClass.class})
public class MyTestClassTest {
@Before
public void setUp() {
mockStatic(Files.class);
}
@Test
public void justToTestMocking_WillDeletePath() throws IOException {
Path path = mock(Path.class);
MyTestClass test = new MyTestClass();
when(Files.exists(path)).thenReturn(true);
test.justToTestMocking(path);
verifyStatic();
Files.delete(path);
}
}