diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 513eec49e..f9aa43e2d 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -105,9 +105,9 @@ dependencies { testImplementation "org.junit.jupiter:junit-jupiter-engine" testImplementation 'org.apache.commons:commons-io:1.3.2' testImplementation 'org.assertj:assertj-core:3.27.3' - testImplementation 'org.mockito:mockito-core:3.12.4' - testImplementation 'org.mockito:mockito-junit-jupiter:3.12.4' - testImplementation "com.squareup.okhttp3:mockwebserver:3.14.9" + testImplementation 'org.mockito:mockito-core:4.11.0' + testImplementation 'org.mockito:mockito-junit-jupiter:4.11.0' + testImplementation "com.squareup.okhttp3:mockwebserver:4.12.0" } diff --git a/test-support/build.gradle b/test-support/build.gradle index db86a010b..769cebf77 100644 --- a/test-support/build.gradle +++ b/test-support/build.gradle @@ -20,9 +20,10 @@ dependencies { optional "org.apache.directory.server:apacheds-server-jndi" optional "org.apache.directory.shared:shared-ldap" optional "com.unboundid:unboundid-ldapsdk" - - provided "junit:junit" testImplementation platform('org.junit:junit-bom') - testImplementation "org.junit.vintage:junit-vintage-engine" testImplementation "org.assertj:assertj-core" + testImplementation "org.junit.jupiter:junit-jupiter:5.12.2" +} +tasks.withType(Test).configureEach { + useJUnitPlatform() } diff --git a/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java b/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java index 3ef90e386..113417788 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java +++ b/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java @@ -22,9 +22,8 @@ import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; -import junit.framework.Assert; - import org.springframework.ldap.core.AttributesMapper; +import org.springframework.util.Assert; /** * Dummy AttributesMapper for testing purposes to check that the received Attributes are @@ -41,16 +40,21 @@ public class AttributeCheckAttributesMapper implements AttributesMapper private String[] absentAttributes = new String[0]; public Object mapFromAttributes(Attributes attributes) throws NamingException { - Assert.assertEquals("Values and attributes need to have the same length ", this.expectedAttributes.length, - this.expectedValues.length); + Assert.isTrue(this.expectedAttributes.length == this.expectedValues.length, + "Values and attributes need to have the same length " + this.expectedAttributes.length + "!=" + + this.expectedValues.length); + for (int i = 0; i < this.expectedAttributes.length; i++) { Attribute attribute = attributes.get(this.expectedAttributes[i]); - Assert.assertNotNull("Attribute " + this.expectedAttributes[i] + " was not present", attribute); - Assert.assertEquals(this.expectedValues[i], attribute.get()); + + Assert.notNull(attribute, "Attribute " + this.expectedAttributes[i] + " was not present"); + + Assert.isTrue(attribute.get().equals(this.expectedValues[i]), "Attribute " + this.expectedAttributes[i] + + " had value " + attribute.get() + " instead of " + this.expectedValues[i]); } for (String absentAttribute : this.absentAttributes) { - Assert.assertNull(attributes.get(absentAttribute)); + Assert.isNull(attributes.get(absentAttribute), "Attribute " + absentAttribute + " was present"); } return new Object(); diff --git a/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java b/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java index 25efbab5c..444ed8a52 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java +++ b/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java @@ -18,10 +18,9 @@ import java.util.Arrays; -import junit.framework.Assert; - import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.util.Assert; /** * Dummy ContextMapper for testing purposes to check that the received Attributes are the @@ -39,16 +38,20 @@ public class AttributeCheckContextMapper implements ContextMapper list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), - new AttributesMapper<>() { - public String mapFromAttributes(Attributes attrs) throws NamingException { - return (String) attrs.get("cn").get(); - } - }); - assertThat(5).isEqualTo(list.size()); + this::mapFromAttributes); + assertThat(list).hasSize(5); + } + + private String mapFromAttributes(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); } } diff --git a/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java b/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java index a05c8213b..01c834a21 100644 --- a/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java +++ b/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java @@ -21,40 +21,39 @@ import javax.naming.NamingException; import javax.naming.directory.Attributes; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.query.LdapQueryBuilder; import static org.assertj.core.api.Assertions.assertThat; -public class EmbeddedLdapServerFactoryBeanTests { +class EmbeddedLdapServerFactoryBeanTests { ClassPathXmlApplicationContext ctx; - @After - public void setup() { + @AfterEach + void setup() { if (this.ctx != null) { this.ctx.close(); } } @Test - public void testServerStartup() throws Exception { + void serverStartup() throws Exception { this.ctx = new ClassPathXmlApplicationContext("/applicationContext-ldifPopulator.xml"); LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class); assertThat(ldapTemplate).isNotNull(); List list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), - new AttributesMapper<>() { - public String mapFromAttributes(Attributes attrs) throws NamingException { - return (String) attrs.get("cn").get(); - } - }); - assertThat(list.size()).isEqualTo(5); + this::mapFromAttributes); + assertThat(list).hasSize(5); + } + + private String mapFromAttributes(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); } } diff --git a/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java b/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java index 1e2e5dfda..f16e7a8a0 100644 --- a/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java +++ b/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java @@ -28,10 +28,9 @@ import com.unboundid.ldap.listener.InMemoryDirectoryServer; import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; import com.unboundid.ldap.listener.InMemoryListenerConfig; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.query.LdapQueryBuilder; @@ -39,17 +38,17 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -public class EmbeddedLdapServerTests { +class EmbeddedLdapServerTests { private int port; - @Before - public void setUp() throws IOException { + @BeforeEach + void setUp() throws IOException { this.port = getFreePort(); } @Test - public void shouldStartAndCloseServer() throws Exception { + void shouldStartAndCloseServer() throws Exception { assertPortIsFree(this.port); EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port); @@ -60,7 +59,7 @@ public void shouldStartAndCloseServer() throws Exception { } @Test - public void shouldStartAndAutoCloseServer() throws Exception { + void shouldStartAndAutoCloseServer() throws Exception { assertPortIsFree(this.port); try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", @@ -71,7 +70,7 @@ public void shouldStartAndAutoCloseServer() throws Exception { } @Test - public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception { + void shouldStartAndCloseServerViaLdapTestUtils() throws Exception { assertPortIsFree(this.port); LdapTestUtils.startEmbeddedServer(this.port, "dc=jayway,dc=se", "jayway"); @@ -82,13 +81,13 @@ public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception { } @Test - public void startWhenNewEmbeddedServerThenException() throws Exception { + void startWhenNewEmbeddedServerThenException() throws Exception { EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start); } @Test - public void startWhenUnstartedThenWorks() throws Exception { + void startWhenUnstartedThenWorks() throws Exception { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se"); config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port)); InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config); @@ -99,7 +98,7 @@ public void startWhenUnstartedThenWorks() throws Exception { } @Test - public void startWhenAlreadyStartedThenFails() throws Exception { + void startWhenAlreadyStartedThenFails() throws Exception { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se"); config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port)); InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config); @@ -111,13 +110,13 @@ public void startWhenAlreadyStartedThenFails() throws Exception { } @Test - public void shouldBuildButNotStartTheServer() { + void shouldBuildButNotStartTheServer() { EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(this.port).build(); assertPortIsFree(this.port); } @Test - public void shouldBuildTheServerWithCustomPort() { + void shouldBuildTheServerWithCustomPort() { EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se") .port(this.port); @@ -129,7 +128,7 @@ public void shouldBuildTheServerWithCustomPort() { } @Test - public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException { + void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException { String tempLogFile = Files.createTempFile("ldap-log-", ".txt").toAbsolutePath().toString(); EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se") @@ -140,11 +139,7 @@ public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOExceptio server.start(); ldapTemplate("dc=jayway,dc=se", this.port) - .search(LdapQueryBuilder.query().where("objectclass").is("person"), new AttributesMapper<>() { - public String mapFromAttributes(Attributes attrs) throws NamingException { - return (String) attrs.get("cn").get(); - } - }); + .search(LdapQueryBuilder.query().where("objectclass").is("person"), this::mapFromAttributes); } assertThat(Path.of(tempLogFile)) @@ -185,4 +180,8 @@ static LdapTemplate ldapTemplate(String base, int port) { return new LdapTemplate(ctx); } + private String mapFromAttributes(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); + } + } diff --git a/test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java b/test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java index 505094571..e68360db5 100644 --- a/test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java +++ b/test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java @@ -21,40 +21,39 @@ import javax.naming.NamingException; import javax.naming.directory.Attributes; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.query.LdapQueryBuilder; import static org.assertj.core.api.Assertions.assertThat; -public class TestContextSourceFactoryBeanTests { +class TestContextSourceFactoryBeanTests { ClassPathXmlApplicationContext ctx; - @After - public void setup() { + @AfterEach + void setup() { if (this.ctx != null) { this.ctx.close(); } } @Test - public void testServerStartup() throws Exception { + void serverStartup() throws Exception { this.ctx = new ClassPathXmlApplicationContext("/applicationContext-testContextSource.xml"); LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class); assertThat(ldapTemplate).isNotNull(); List list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), - new AttributesMapper<>() { - public String mapFromAttributes(Attributes attrs) throws NamingException { - return (String) attrs.get("cn").get(); - } - }); - assertThat(list.size()).isEqualTo(5); + this::mapFromAttributes); + assertThat(list).hasSize(5); + } + + private String mapFromAttributes(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); } }