-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathTestHelper.java
More file actions
49 lines (43 loc) · 1.51 KB
/
TestHelper.java
File metadata and controls
49 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package utils;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.List;
import java.util.function.BooleanSupplier;
public class TestHelper {
public static String getFixtureContent(String fixture) throws IOException, URISyntaxException {
return new String(Files.readAllBytes(Paths.get(TestHelper.class.getResource(fixture).toURI())));
}
public static List<String> getFixtureLines(String fixture) {
try {
return Files.readAllLines(Paths.get(TestHelper.class.getResource(fixture).toURI()));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static void setFieldInConfig(Object target, String fieldName, Object value) {
try {
Field field = target.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(target, value);
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void assertWithTimeout(BooleanSupplier predicate, Duration timeout) {
Duration sleepTime = Duration.ofMillis(10);
long count = timeout.toMillis() / sleepTime.toMillis();
while (count-- > 0 && !predicate.getAsBoolean()) {
try {
Thread.sleep(sleepTime.toMillis());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
assertTrue(predicate.getAsBoolean());
}
}