This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new a593c2f5c Rewrite ObjectUtilsTest.testWaitDuration() with mocking.
a593c2f5c is described below
commit a593c2f5c12c4a851908f043540e1908ede1b179
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Jul 30 07:19:38 2026 -0400
Rewrite ObjectUtilsTest.testWaitDuration() with mocking.
---
.../org/apache/commons/lang3/ObjectUtilsTest.java | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index 8afee673d..a58b1dbec 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -19,6 +19,9 @@
import static
org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
import static
org.apache.commons.lang3.LangAssertions.assertNullPointerException;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mockStatic;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -54,7 +57,9 @@
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.mutable.MutableObject;
import org.apache.commons.lang3.text.StrBuilder;
+import org.apache.commons.lang3.time.DurationUtils;
import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
/**
* Tests {@link ObjectUtils}.
@@ -891,12 +896,17 @@ void testToString_Supplier_Supplier() {
@Test
void testWaitDuration() throws InterruptedException {
final Object lock = new Object();
- final long start = System.nanoTime();
- synchronized (lock) {
- ObjectUtils.wait(lock, Duration.ofMillis(100));
+ try (MockedStatic<DurationUtils> mocked =
mockStatic(DurationUtils.class)) {
+ // Let zeroIfNull run for real so the Duration reaches accept
unchanged.
+ mocked.when(() ->
DurationUtils.zeroIfNull(any())).thenCallRealMethod();
+ // accept is mocked to do nothing, so no actual waiting occurs.
+ final int millis = 1_500;
+ synchronized (lock) {
+ ObjectUtils.wait(lock, Duration.ofMillis(millis));
+ }
+ // Verify accept was called with the expected Duration.
+ mocked.verify(() -> DurationUtils.accept(any(),
eq(Duration.ofMillis(millis))));
}
- final long elapsed = System.nanoTime() - start;
- assertTrue(elapsed >= Duration.ofMillis(100).toNanos());
}
@Test