Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 7489085

Browse files
committed
Changed Wait's Waiter field from a static reference to a supplier
This allows for testability as well as changing the behavior of the Wait class with custom behavior. The default behavior is thread safe because each call gets a new instance of Waiter.
1 parent bdb4b94 commit 7489085

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/waiting/CurrentThreadSleeper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @see Sleeper
77
* @since 2.0
88
*/
9-
class CurrentThreadSleeper implements Sleeper {
9+
public class CurrentThreadSleeper implements Sleeper {
1010

1111
@Override
1212
public void sleep(long milliseconds) throws InterruptionException {

webtester-core/src/main/java/info/novatec/testit/webtester/waiting/Sleeper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @since 2.0
77
*/
8-
interface Sleeper {
8+
public interface Sleeper {
99
/**
1010
* Sleep for the given amount of milliseconds.
1111
*

webtester-core/src/main/java/info/novatec/testit/webtester/waiting/Wait.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package info.novatec.testit.webtester.waiting;
22

33
import java.util.concurrent.TimeUnit;
4+
import java.util.function.Supplier;
5+
6+
import lombok.Setter;
47

58
import info.novatec.testit.webtester.pagefragments.PageFragment;
69

@@ -22,11 +25,18 @@
2225
*/
2326
public final class Wait {
2427

28+
/** The default {@link Waiter} supplier. Generates a new {@link Waiter} for each call. */
29+
public static final Supplier<Waiter> DEFAULT_WAITER = Waiter::new;
30+
2531
/**
26-
* The {@link Waiter} to use when executing wait operations.
27-
* This field is package-private in order to allow for it to be tests.
32+
* A supplier used to get a {@link Waiter} instance to use when executing any wait operations.
33+
* The supplier can be changed externally to customize the waiting behavior.
34+
* Since this is a static field you should keep in mind that this will have an JVM global effect!
35+
* <p>
36+
* The default supplier is {@link #DEFAULT_WAITER}.
2837
*/
29-
private static Waiter waiter = new Waiter();
38+
@Setter
39+
private static Supplier<Waiter> waiter = DEFAULT_WAITER;
3040

3141
private Wait() {
3242
// utility class constructor
@@ -45,7 +55,7 @@ private Wait() {
4555
* @since 2.0
4656
*/
4757
public static ConfiguredWait withTimeoutOf(int timeout) {
48-
return new ConfiguredWait(waiter, new WaitConfig().setTimeout(timeout));
58+
return new ConfiguredWait(waiter.get(), new WaitConfig().setTimeout(timeout));
4959
}
5060

5161
/**
@@ -62,7 +72,7 @@ public static ConfiguredWait withTimeoutOf(int timeout) {
6272
* @since 2.0
6373
*/
6474
public static ConfiguredWait withTimeoutOf(int timeout, TimeUnit timeUnit) {
65-
return new ConfiguredWait(waiter, new WaitConfig().setTimeout(timeout).setTimeUnit(timeUnit));
75+
return new ConfiguredWait(waiter.get(), new WaitConfig().setTimeout(timeout).setTimeUnit(timeUnit));
6676
}
6777

6878
/**
@@ -77,7 +87,7 @@ public static ConfiguredWait withTimeoutOf(int timeout, TimeUnit timeUnit) {
7787
* @since 2.0
7888
*/
7989
public static <T> WaitUntil<T> until(T object) {
80-
return new WaitUntil<>(waiter, new WaitConfig(), object);
90+
return new WaitUntil<>(waiter.get(), new WaitConfig(), object);
8191
}
8292

8393
/**
@@ -92,7 +102,7 @@ public static <T> WaitUntil<T> until(T object) {
92102
* @since 2.0
93103
*/
94104
public static <T extends PageFragment> WaitUntil<T> until(T fragment) {
95-
return new WaitUntil<>(waiter, WaitConfig.from(fragment), fragment);
105+
return new WaitUntil<>(waiter.get(), WaitConfig.from(fragment), fragment);
96106
}
97107

98108
/**
@@ -109,7 +119,7 @@ public static <T extends PageFragment> WaitUntil<T> until(T fragment) {
109119
* @since 2.0
110120
*/
111121
public static void exactly(long duration, TimeUnit timeUnit) {
112-
waiter.waitExactly(duration, timeUnit);
122+
waiter.get().waitExactly(duration, timeUnit);
113123
}
114124

115125
}

webtester-core/src/main/java/info/novatec/testit/webtester/waiting/Waiter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
@Slf4j
1919
@Getter(AccessLevel.PROTECTED)
20-
class Waiter {
20+
public class Waiter {
2121

2222
/**
2323
* The {@link Sleeper} to use when actually needing to pass time.

webtester-core/src/test/java/info/novatec/testit/webtester/waiting/WaitTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import org.mockito.Mock;
1515
import org.mockito.runners.MockitoJUnitRunner;
1616

17-
import utils.TestUtils;
18-
1917
import info.novatec.testit.webtester.browser.Browser;
2018
import info.novatec.testit.webtester.config.Configuration;
2119
import info.novatec.testit.webtester.pagefragments.PageFragment;
@@ -29,17 +27,15 @@ public static abstract class AbstractWaitTest {
2927

3028
@Mock
3129
Waiter waiter;
32-
Waiter originalWaiter;
3330

3431
@Before
3532
public void rememberAndReplaceOriginalWaiter() {
36-
originalWaiter = TestUtils.getStaticFieldValue(Wait.class, "waiter");
37-
TestUtils.setStaticFieldValue(Wait.class, "waiter", waiter);
33+
Wait.setWaiter(() -> waiter);
3834
}
3935

4036
@After
4137
public void restoreOriginalWaiter() {
42-
TestUtils.setStaticFieldValue(Wait.class, "waiter", originalWaiter);
38+
Wait.setWaiter(Wait.DEFAULT_WAITER);
4339
}
4440

4541
}

0 commit comments

Comments
 (0)