This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch feature/junit5 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-hamcrest.git
commit ff14bc72a329314e3c8ea3c9cc1a22f19724e9e4 Author: Konrad Windszus <[email protected]> AuthorDate: Fri Jun 20 15:12:08 2025 +0200 SLING-12836 Upgrade to JUnit5 --- pom.xml | 10 +++---- .../org/apache/sling/hamcrest/MapUtilTest.java | 31 ++++++++++---------- .../hamcrest/ResourceCollectionMatchersTest.java | 26 ++++++++--------- .../hamcrest/ResourceIteratorMatchersTest.java | 19 +++++++------ .../sling/hamcrest/ResourceMatchersTest.java | 33 +++++++++++----------- 5 files changed, 60 insertions(+), 59 deletions(-) diff --git a/pom.xml b/pom.xml index b228bc0..e08d747 100644 --- a/pom.xml +++ b/pom.xml @@ -61,20 +61,20 @@ <!-- testing dependencies --> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.testing.sling-mock</artifactId> - <version>3.4.14</version> + <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId> + <version>3.5.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.testing.logging-mock</artifactId> - <version>1.0.0</version> + <version>2.0.0</version> <scope>test</scope> </dependency> diff --git a/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java b/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java index a7e0427..3e62888 100644 --- a/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java +++ b/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java @@ -21,51 +21,50 @@ package org.apache.sling.hamcrest; import java.util.Collections; import java.util.Map; -import com.google.common.collect.ImmutableMap; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -public class MapUtilTest { +class MapUtilTest { - private static final ImmutableMap<String, Object> EXPECTED_MAP = - ImmutableMap.<String, Object>of("param1", "var1", "param2", 123, "param3", true); + private static final Map<String, Object> EXPECTED_MAP = Map.of("param1", "var1", "param2", 123, "param3", true); @Test - public void testMapObjectVarargs() { + void testMapObjectVarargs() { Map<String, Object> convertedMap = MapUtil.toMap("param1", "var1", "param2", 123, "param3", true); assertEquals(EXPECTED_MAP, convertedMap); } @Test - public void testMapObjectMap() { + void testMapObjectMap() { Map<String, Object> convertedMap = MapUtil.toMap(EXPECTED_MAP); assertEquals(EXPECTED_MAP, convertedMap); } - @Test(expected = IllegalArgumentException.class) - public void testMapObjectVarArgs_NotMap() { - MapUtil.toMap("param1", "var1", "param2", 123, "param3"); + @Test + void testMapObjectVarArgs_NotMap() { + assertThrows(IllegalArgumentException.class, () -> MapUtil.toMap("param1", "var1", "param2", 123, "param3")); } @Test - public void testMapObjectVarargs_EmptyArgs() { + void testMapObjectVarargs_EmptyArgs() { Map<String, Object> convertedMap = MapUtil.toMap(); assertEquals(Collections.emptyMap(), convertedMap); } @Test - public void testMapObjectVarargs_Null() { + void testMapObjectVarargs_Null() { Map<String, Object> convertedMap = MapUtil.toMap((Object[]) null); assertEquals(Collections.emptyMap(), convertedMap); } - @Test(expected = IllegalArgumentException.class) - public void testMapObjectVarArgs_OddNumberOfArgs() { - MapUtil.toMap("param1", "var1", "param2", 123, "param3"); + @Test + void testMapObjectVarArgs_OddNumberOfArgs() { + assertThrows(IllegalArgumentException.class, () -> MapUtil.toMap("param1", "var1", "param2", 123, "param3")); } } diff --git a/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java b/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java index ce5cb83..f83b99c 100644 --- a/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java +++ b/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java @@ -20,38 +20,38 @@ package org.apache.sling.hamcrest; import java.util.List; -import com.google.common.collect.ImmutableList; import org.apache.sling.api.resource.Resource; -import org.apache.sling.testing.mock.sling.junit.SlingContext; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.apache.sling.testing.mock.sling.junit5.SlingContext; +import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.not; -public class ResourceCollectionMatchersTest { +@ExtendWith(SlingContextExtension.class) +class ResourceCollectionMatchersTest { - @Rule - public final SlingContext context = new SlingContext(); + private final SlingContext context = new SlingContext(); private List<Resource> list; - @Before - public void setUp() { - list = ImmutableList.of( + @BeforeEach + void setUp() { + list = List.of( context.create().resource("/content/1"), context.create().resource("/content/2"), context.create().resource("/content/3")); } @Test - public void testMatch() { + void testMatch() { assertThat(list, ResourceCollectionMatchers.paths("/content/1", "/content/2", "/content/3")); } @Test - public void testMisMatch() { + void testMisMatch() { assertThat(list, not(ResourceCollectionMatchers.paths("/content/1", "/content/2", "/content/3", "/content/4"))); assertThat(list, not(ResourceCollectionMatchers.paths("/content/1", "/content/2"))); assertThat(list, not(ResourceCollectionMatchers.paths("/content/1", "/content/3", "/content/2"))); diff --git a/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java b/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java index e7bf476..57ab284 100644 --- a/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java +++ b/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java @@ -22,22 +22,23 @@ import java.util.List; import com.google.common.collect.ImmutableList; import org.apache.sling.api.resource.Resource; -import org.apache.sling.testing.mock.sling.junit.SlingContext; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.apache.sling.testing.mock.sling.junit5.SlingContext; +import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.not; +@ExtendWith(SlingContextExtension.class) public class ResourceIteratorMatchersTest { - @Rule - public final SlingContext context = new SlingContext(); + private final SlingContext context = new SlingContext(); private List<Resource> list; - @Before + @BeforeEach public void setUp() { list = ImmutableList.of( context.create().resource("/content/1"), @@ -46,12 +47,12 @@ public class ResourceIteratorMatchersTest { } @Test - public void testMatch() { + void testMatch() { assertThat(list.iterator(), ResourceIteratorMatchers.paths("/content/1", "/content/2", "/content/3")); } @Test - public void testMisMatch() { + void testMisMatch() { assertThat( list.iterator(), not(ResourceIteratorMatchers.paths("/content/1", "/content/2", "/content/3", "/content/4"))); diff --git a/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java b/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java index c84bc5f..470a88a 100644 --- a/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java +++ b/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java @@ -24,23 +24,24 @@ import com.google.common.collect.ImmutableMap; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.SyntheticResource; -import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.apache.sling.testing.mock.sling.junit5.SlingContext; +import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension; import org.hamcrest.Matchers; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.MatcherAssert.assertThat; +@ExtendWith(SlingContextExtension.class) public class ResourceMatchersTest { // only defined in newer versions of Sling API private static final String PROPERTY_RESOURCE_SUPER_TYPE = "sling:resourceSuperType"; - @Rule - public final SlingContext context = new SlingContext(); + private final SlingContext context = new SlingContext(); @Test - public void testResourceType() { + void testResourceType() { context.build() .resource( "/resource", @@ -60,7 +61,7 @@ public class ResourceMatchersTest { } @Test - public void testResourceTypeOrDerived() { + void testResourceTypeOrDerived() { context.build() .resource( "/resource", @@ -83,7 +84,7 @@ public class ResourceMatchersTest { } @Test - public void testPath() { + void testPath() { context.build().resource("/resource"); Resource resource = context.resourceResolver().getResource("/resource"); @@ -92,7 +93,7 @@ public class ResourceMatchersTest { } @Test - public void testName() { + void testName() { context.build().resource("/resource"); Resource resource = context.resourceResolver().getResource("/resource"); @@ -101,7 +102,7 @@ public class ResourceMatchersTest { } @Test - public void testProps() { + void testProps() { context.build() .resource( "/resource", @@ -137,7 +138,7 @@ public class ResourceMatchersTest { } @Test - public void testPropsVarargs() { + void testPropsVarargs() { context.build() .resource( "/resource", "key1", "value1", "key2", true, "key3", new int[] {1, 2, 3}, "key4", "otherValue"); @@ -166,7 +167,7 @@ public class ResourceMatchersTest { } @Test - public void testHasChildren() { + void testHasChildren() { context.build().resource("/parent").resource("child1").resource("/parent/child2"); Resource resource = context.resourceResolver().getResource("/parent"); @@ -174,7 +175,7 @@ public class ResourceMatchersTest { } @Test - public void testNameAndProps() { + void testNameAndProps() { context.build().resource("/resource", "key1", "value1", "key2", new String[] {"item1"}, "key3", "value3"); Map<String, Object> expectedProperties = ImmutableMap.<String, Object>builder() @@ -197,7 +198,7 @@ public class ResourceMatchersTest { } @Test - public void testNameAndPropsVarargs() { + void testNameAndPropsVarargs() { context.build().resource("/resource", "key1", "value1", "key2", "value2", "key3", "value3"); Object[] expectedProperties = new Object[] { @@ -220,7 +221,7 @@ public class ResourceMatchersTest { } @Test - public void testContainsChildrenInAnyOrder() { + void testContainsChildrenInAnyOrder() { context.build().resource("/parent").resource("child1").resource("/parent/child2"); Resource resource = context.resourceResolver().getResource("/parent"); @@ -230,7 +231,7 @@ public class ResourceMatchersTest { } @Test - public void testContainsChildren() { + void testContainsChildren() { context.build().resource("/parent").resource("child1").resource("/parent/child2"); Resource resource = context.resourceResolver().getResource("/parent");
