From 33dc81685779e5be875e4110dcf9a0f0a7d02d7a Mon Sep 17 00:00:00 2001 From: Vincent Stradiot Date: Sat, 13 Dec 2025 08:39:51 +0100 Subject: [PATCH 1/3] Extend and improve some test cases CollectionUtilsTests: - Every case must be run with a fresh enumeration - Extend test cases with non-existing element ExponentialBackOffTests: - Rename test case to better reflect what it does - Extend test case with jitter functionality BooleanComparatorTests: - Extend test cases to be more exhaustive Signed-off-by: Vincent Stradiot --- .../util/CollectionUtilsTests.java | 6 +-- .../util/ExponentialBackOffTests.java | 47 ++++++++++++++++++- .../comparator/BooleanComparatorTests.java | 8 ++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java b/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java index 2123127d82c..a14aea4ec57 100644 --- a/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java @@ -116,9 +116,9 @@ void containsWithEnumeration() { assertThat(CollectionUtils.contains(Collections.enumeration(List.of()), "myElement")).isFalse(); List list = Arrays.asList("myElement", null); - Enumeration enumeration = Collections.enumeration(list); - assertThat(CollectionUtils.contains(enumeration, "myElement")).isTrue(); - assertThat(CollectionUtils.contains(enumeration, null)).isTrue(); + assertThat(CollectionUtils.contains(Collections.enumeration(list), "myElement")).isTrue(); + assertThat(CollectionUtils.contains(Collections.enumeration(list), null)).isTrue(); + assertThat(CollectionUtils.contains(Collections.enumeration(list), "nonExistingElement")).isFalse(); } @Test diff --git a/spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java b/spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java index 40271f5c744..142e2c714f3 100644 --- a/spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java +++ b/spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java @@ -81,7 +81,7 @@ void maxIntervalReached() { } @Test - void maxAttemptsReached() { + void maxElapsedTimeReached() { ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0); backOff.setMaxElapsedTime(4000L); @@ -162,4 +162,49 @@ void maxAttempts() { assertThat(delays).containsExactly(1000L, 2000L, 4000L, 8000L, 10000L, 10000L, BackOffExecution.STOP); } + @Test + void withJitter_ThenJitterIsApplied() { + // GIVEN + ExponentialBackOff backOff = new ExponentialBackOff(); + backOff.setInitialInterval(1000L); + backOff.setJitter(100L); + backOff.setMaxInterval(20000L); + backOff.setMultiplier(2.0); + + BackOffExecution execution = backOff.start(); + + // WHEN + List delays = IntStream.range(0, 7).mapToObj(i -> execution.nextBackOff()).toList(); + + // THEN + assertThat(delays).hasSize(7); + assertThat(delays) + .withFailMessage("Jitter was not applied") + .isNotEqualTo(List.of(1000L, 2000L, 4000L, 8000L, 16000L, 20000L, 20000L)); + } + + @Test + void withJitter_ThenDelaysStayInExpectedRanges() { + // GIVEN + ExponentialBackOff backOff = new ExponentialBackOff(); + backOff.setInitialInterval(1000L); + backOff.setJitter(100L); // 10% of initial interval + backOff.setMaxInterval(20000L); + backOff.setMultiplier(2.0); + + BackOffExecution execution = backOff.start(); + + // WHEN + List delays = IntStream.range(0, 7).mapToObj(i -> execution.nextBackOff()).toList(); + + // THEN + assertThat(delays.get(0)).isBetween(1000L, 1100L); // 1000 + 10% (never below initial interval) + assertThat(delays.get(1)).isBetween(1800L, 2200L); // 2000 +/- 10% + assertThat(delays.get(2)).isBetween(3600L, 4400L); // 4000 +/- 10% + assertThat(delays.get(3)).isBetween(7200L, 8800L); // 8000 +/- 10% + assertThat(delays.get(4)).isBetween(14400L, 17600L); // 16000 +/- 10% + assertThat(delays.get(5)).isBetween(18000L, 20000L); // 20000 - 10% (max interval) + assertThat(delays.get(6)).isBetween(18000L, 20000L); // 20000 - 10% (max interval) + } + } diff --git a/spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java index c8a5d70ce27..f05fd874202 100644 --- a/spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java +++ b/spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java @@ -37,28 +37,36 @@ class BooleanComparatorTests { void shouldCompareWithTrueLow() { Comparator c = new BooleanComparator(true); assertThat(c.compare(true, false)).isLessThan(0); + assertThat(c.compare(false, true)).isGreaterThan(0); assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0); + assertThat(c.compare(Boolean.FALSE, Boolean.FALSE)).isEqualTo(0); } @Test void shouldCompareWithTrueHigh() { Comparator c = new BooleanComparator(false); assertThat(c.compare(true, false)).isGreaterThan(0); + assertThat(c.compare(false, true)).isLessThan(0); assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0); + assertThat(c.compare(Boolean.FALSE, Boolean.FALSE)).isEqualTo(0); } @Test void shouldCompareFromTrueLow() { Comparator c = BooleanComparator.TRUE_LOW; assertThat(c.compare(true, false)).isLessThan(0); + assertThat(c.compare(false, true)).isGreaterThan(0); assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0); + assertThat(c.compare(Boolean.FALSE, Boolean.FALSE)).isEqualTo(0); } @Test void shouldCompareFromTrueHigh() { Comparator c = BooleanComparator.TRUE_HIGH; assertThat(c.compare(true, false)).isGreaterThan(0); + assertThat(c.compare(false, true)).isLessThan(0); assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0); + assertThat(c.compare(Boolean.FALSE, Boolean.FALSE)).isEqualTo(0); } } From 00439cfd783b1a6948838e8ebbe5ee6e3778681c Mon Sep 17 00:00:00 2001 From: Vincent Stradiot Date: Mon, 15 Dec 2025 21:46:24 +0100 Subject: [PATCH 2/3] AssertTests: add test case AssertTests.isNullWithNonNullObject Signed-off-by: Vincent Stradiot --- .../src/test/java/org/springframework/util/AssertTests.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-core/src/test/java/org/springframework/util/AssertTests.java b/spring-core/src/test/java/org/springframework/util/AssertTests.java index 1983d07309f..3082bb04ac9 100644 --- a/spring-core/src/test/java/org/springframework/util/AssertTests.java +++ b/spring-core/src/test/java/org/springframework/util/AssertTests.java @@ -109,6 +109,12 @@ void isNullWithMessage() { Assert.isNull(null, "Bla"); } + @Test + void isNullWithNonNullObject() { + assertThatIllegalArgumentException().isThrownBy(() -> Assert.isNull("foo", "Bla")) + .withMessage("Bla"); + } + @Test void isNullWithMessageSupplier() { Assert.isNull(null, () -> "enigma"); From 017aa86f3ca53c2f41cd134ce636eb19a3e9461c Mon Sep 17 00:00:00 2001 From: Vincent Stradiot Date: Mon, 15 Dec 2025 21:48:02 +0100 Subject: [PATCH 3/3] BeanFactoryUtilsTests: add test cases Add test cases that cover BeanFactoryUtils.uniqueBean method Signed-off-by: Vincent Stradiot --- .../beans/factory/BeanFactoryUtilsTests.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java index 4ab5ced5b1b..2899180443c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java @@ -43,6 +43,7 @@ import org.springframework.util.ObjectUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifiedResource; /** @@ -171,6 +172,41 @@ void testFindsBeansOfTypeWithStaticFactory() { assertThat(beans.get("&t4")).isEqualTo(t4); } + @Test + void testBeanOfType_GivenSingleBean_ThenReturnsBean() { + StaticListableBeanFactory lbf = new StaticListableBeanFactory(); + TestBean t1 = new TestBean(); + lbf.addBean("t1", t1); + + ITestBean actual = BeanFactoryUtils.beanOfType(lbf, ITestBean.class); + + assertThat(actual).isSameAs(t1); + } + + @Test + void testBeanOfType_GivenMultipleBeans_ThenThrowsException() { + StaticListableBeanFactory lbf = new StaticListableBeanFactory(); + TestBean t1 = new TestBean(); + TestBean t2 = new TestBean(); + DummyFactory t3 = new DummyFactory(); + lbf.addBean("t1", t1); + lbf.addBean("t2", t2); + lbf.addBean("t3", t3); + + assertThatExceptionOfType(NoUniqueBeanDefinitionException.class) + .isThrownBy(() -> BeanFactoryUtils.beanOfType(lbf, ITestBean.class)) + .withMessage("No qualifying bean of type 'org.springframework.beans.testfixture.beans.ITestBean' available: expected single matching bean but found 3: t1,t2,t3"); + } + + @Test + void testBeanOfType_GivenBeanDoesNotExist_ThenThrowsException() { + StaticListableBeanFactory lbf = new StaticListableBeanFactory(); + + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> BeanFactoryUtils.beanOfType(lbf, ITestBean.class)) + .withMessage("No qualifying bean of type 'org.springframework.beans.testfixture.beans.ITestBean' available"); + } + @Test void testFindsBeansOfTypeWithDefaultFactory() { Object test3 = this.listableBeanFactory.getBean("test3");