Skip to content

Instantly share code, notes, and snippets.

public class FileCreatorTestWithCustomValidator {
private FileCreator fileCreator = new FileCreator();
private String fileName = "file.txt";
@Before
public void setUp() throws IOException {
Files.deleteIfExists(Paths.get(fileName));
}
@Test
public void shouldCreateFile() throws InterruptedException {
// when
fileCreator.createFile(fileName);
// then
boolean fileExists = checkThatFileExists(fileName);
assertThat(fileExists, is(true));
}
private boolean checkThatFileExists(String fileName)
throws InterruptedException {
for (int i = 0; i < 100; i++) {
Path path = Paths.get(fileName);
try {
assertThat(path, exists());
return true;
} catch (AssertionError e) {
// ignore exception
}
Thread.sleep(100);
}
throw new AssertionError("Timeout exceeded");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment