Skip to content

Commit ca10190

Browse files
committed
test-support module to JUnit5
Issue #1058 Signed-off-by: etrandafir93 <[email protected]>
1 parent 09b2ad3 commit ca10190

File tree

10 files changed

+69
-50
lines changed

10 files changed

+69
-50
lines changed

build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ buildscript {
1616
plugins {
1717
id 'io.spring.antora.generate-antora-yml' version '0.0.1'
1818
id 'org.antora' version '1.0.0'
19+
id("org.openrewrite.rewrite") version "7.6.1"
1920
}
2021

2122
apply plugin: 'io.spring.convention.root'
@@ -104,3 +105,11 @@ allprojects {
104105
}
105106
}
106107
}
108+
109+
rewrite {
110+
activeRecipe("org.openrewrite.java.testing.junit5.JUnit5BestPractices")
111+
dependencies {
112+
rewrite "org.openrewrite.recipe:rewrite-recipe-bom:3.8.0"
113+
rewrite "org.openrewrite.recipe:rewrite-testing-frameworks:3.8.0"
114+
}
115+
}

buildSrc/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ dependencies {
105105
testImplementation "org.junit.jupiter:junit-jupiter-engine"
106106
testImplementation 'org.apache.commons:commons-io:1.3.2'
107107
testImplementation 'org.assertj:assertj-core:3.27.3'
108-
testImplementation 'org.mockito:mockito-core:3.12.4'
109-
testImplementation 'org.mockito:mockito-junit-jupiter:3.12.4'
110-
testImplementation "com.squareup.okhttp3:mockwebserver:3.14.9"
108+
testImplementation 'org.mockito:mockito-core:4.11.0'
109+
testImplementation 'org.mockito:mockito-junit-jupiter:4.11.0'
110+
testImplementation "com.squareup.okhttp3:mockwebserver:4.12.0"
111111
}
112112

113113

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
version=3.3.0-SNAPSHOT
22
springJavaformatVersion=0.0.38
33
org.gradle.caching=true
4+
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m

test-support/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ dependencies {
1313

1414
implementation "com.google.code.typica:typica"
1515

16+
implementation "org.junit.jupiter:junit-jupiter:5.12.2"
17+
1618
optional "org.apache.directory.server:apacheds-core-entry"
1719
optional "org.apache.directory.server:apacheds-core"
1820
optional "org.apache.directory.server:apacheds-protocol-ldap"
1921
optional "org.apache.directory.server:apacheds-protocol-shared"
2022
optional "org.apache.directory.server:apacheds-server-jndi"
2123
optional "org.apache.directory.shared:shared-ldap"
2224
optional "com.unboundid:unboundid-ldapsdk"
23-
24-
provided "junit:junit"
2525
testImplementation platform('org.junit:junit-bom')
26-
testImplementation "org.junit.vintage:junit-vintage-engine"
2726
testImplementation "org.assertj:assertj-core"
2827
}
28+
tasks.withType(Test).configureEach {
29+
useJUnitPlatform()
30+
}

test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import javax.naming.directory.Attribute;
2323
import javax.naming.directory.Attributes;
2424

25-
import junit.framework.Assert;
26-
2725
import org.springframework.ldap.core.AttributesMapper;
2826

27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
import static org.junit.jupiter.api.Assertions.assertNull;
30+
2931
/**
3032
* Dummy AttributesMapper for testing purposes to check that the received Attributes are
3133
* the expected ones.
@@ -41,16 +43,17 @@ public class AttributeCheckAttributesMapper implements AttributesMapper<Object>
4143
private String[] absentAttributes = new String[0];
4244

4345
public Object mapFromAttributes(Attributes attributes) throws NamingException {
44-
Assert.assertEquals("Values and attributes need to have the same length ", this.expectedAttributes.length,
45-
this.expectedValues.length);
46+
assertEquals(this.expectedAttributes.length,
47+
this.expectedValues.length,
48+
"Values and attributes need to have the same length ");
4649
for (int i = 0; i < this.expectedAttributes.length; i++) {
4750
Attribute attribute = attributes.get(this.expectedAttributes[i]);
48-
Assert.assertNotNull("Attribute " + this.expectedAttributes[i] + " was not present", attribute);
49-
Assert.assertEquals(this.expectedValues[i], attribute.get());
51+
assertNotNull(attribute, "Attribute " + this.expectedAttributes[i] + " was not present");
52+
assertEquals(this.expectedValues[i], attribute.get());
5053
}
5154

5255
for (String absentAttribute : this.absentAttributes) {
53-
Assert.assertNull(attributes.get(absentAttribute));
56+
assertNull(attributes.get(absentAttribute));
5457
}
5558

5659
return new Object();

test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
import java.util.Arrays;
2020

21-
import junit.framework.Assert;
22-
2321
import org.springframework.ldap.core.ContextMapper;
22+
2423
import org.springframework.ldap.core.DirContextAdapter;
2524

25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
27+
import static org.junit.jupiter.api.Assertions.assertNull;
28+
2629
/**
2730
* Dummy ContextMapper for testing purposes to check that the received Attributes are the
2831
* expected ones.
@@ -39,16 +42,17 @@ public class AttributeCheckContextMapper implements ContextMapper<DirContextAdap
3942

4043
public DirContextAdapter mapFromContext(Object ctx) {
4144
DirContextAdapter adapter = (DirContextAdapter) ctx;
42-
Assert.assertEquals("Values and attributes need to have the same length ", this.expectedAttributes.length,
43-
this.expectedValues.length);
45+
assertEquals(this.expectedAttributes.length,
46+
this.expectedValues.length,
47+
"Values and attributes need to have the same length ");
4448
for (int i = 0; i < this.expectedAttributes.length; i++) {
4549
String attributeValue = adapter.getStringAttribute(this.expectedAttributes[i]);
46-
Assert.assertNotNull("Attribute " + this.expectedAttributes[i] + " was not present", attributeValue);
47-
Assert.assertEquals(this.expectedValues[i], attributeValue);
50+
assertNotNull(attributeValue, "Attribute " + this.expectedAttributes[i] + " was not present");
51+
assertEquals(this.expectedValues[i], attributeValue);
4852
}
4953

5054
for (String absentAttribute : this.absentAttributes) {
51-
Assert.assertNull(adapter.getStringAttribute(absentAttribute));
55+
assertNull(adapter.getStringAttribute(absentAttribute));
5256
}
5357

5458
return adapter;

test-support/src/test/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBeanTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121
import javax.naming.NamingException;
2222
import javax.naming.directory.Attributes;
2323

24-
import org.junit.Test;
25-
2624
import org.springframework.context.support.ClassPathXmlApplicationContext;
25+
26+
import org.junit.jupiter.api.Test;
2727
import org.springframework.ldap.core.AttributesMapper;
2828
import org.springframework.ldap.core.LdapTemplate;
2929
import org.springframework.ldap.query.LdapQueryBuilder;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
3232

33-
public class EmbeddedLdapServerFactoryBeanTests {
33+
class EmbeddedLdapServerFactoryBeanTests {
3434

3535
@Test
36-
public void testServerStartup() throws Exception {
36+
void serverStartup() throws Exception {
3737
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
3838
LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
3939
assertThat(ldapTemplate).isNotNull();

test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import javax.naming.NamingException;
2222
import javax.naming.directory.Attributes;
2323

24-
import org.junit.After;
25-
import org.junit.Test;
24+
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.context.support.ClassPathXmlApplicationContext;
2828
import org.springframework.ldap.core.AttributesMapper;
@@ -31,19 +31,19 @@
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
3333

34-
public class EmbeddedLdapServerFactoryBeanTests {
34+
class EmbeddedLdapServerFactoryBeanTests {
3535

3636
ClassPathXmlApplicationContext ctx;
3737

38-
@After
39-
public void setup() {
38+
@AfterEach
39+
void setup() {
4040
if (this.ctx != null) {
4141
this.ctx.close();
4242
}
4343
}
4444

4545
@Test
46-
public void testServerStartup() throws Exception {
46+
void serverStartup() throws Exception {
4747
this.ctx = new ClassPathXmlApplicationContext("/applicationContext-ldifPopulator.xml");
4848
LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class);
4949
assertThat(ldapTemplate).isNotNull();

test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
2929
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
3030
import com.unboundid.ldap.listener.InMemoryListenerConfig;
31-
import org.junit.Before;
32-
import org.junit.Test;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
3333

3434
import org.springframework.ldap.core.AttributesMapper;
3535
import org.springframework.ldap.core.LdapTemplate;
@@ -39,17 +39,17 @@
3939
import static org.assertj.core.api.Assertions.assertThat;
4040
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4141

42-
public class EmbeddedLdapServerTests {
42+
class EmbeddedLdapServerTests {
4343

4444
private int port;
4545

46-
@Before
47-
public void setUp() throws IOException {
46+
@BeforeEach
47+
void setUp() throws IOException {
4848
this.port = getFreePort();
4949
}
5050

5151
@Test
52-
public void shouldStartAndCloseServer() throws Exception {
52+
void shouldStartAndCloseServer() throws Exception {
5353
assertPortIsFree(this.port);
5454

5555
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port);
@@ -60,7 +60,7 @@ public void shouldStartAndCloseServer() throws Exception {
6060
}
6161

6262
@Test
63-
public void shouldStartAndAutoCloseServer() throws Exception {
63+
void shouldStartAndAutoCloseServer() throws Exception {
6464
assertPortIsFree(this.port);
6565

6666
try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se",
@@ -71,7 +71,7 @@ public void shouldStartAndAutoCloseServer() throws Exception {
7171
}
7272

7373
@Test
74-
public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
74+
void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
7575
assertPortIsFree(this.port);
7676

7777
LdapTestUtils.startEmbeddedServer(this.port, "dc=jayway,dc=se", "jayway");
@@ -82,13 +82,13 @@ public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
8282
}
8383

8484
@Test
85-
public void startWhenNewEmbeddedServerThenException() throws Exception {
85+
void startWhenNewEmbeddedServerThenException() throws Exception {
8686
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port);
8787
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
8888
}
8989

9090
@Test
91-
public void startWhenUnstartedThenWorks() throws Exception {
91+
void startWhenUnstartedThenWorks() throws Exception {
9292
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
9393
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port));
9494
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
@@ -99,7 +99,7 @@ public void startWhenUnstartedThenWorks() throws Exception {
9999
}
100100

101101
@Test
102-
public void startWhenAlreadyStartedThenFails() throws Exception {
102+
void startWhenAlreadyStartedThenFails() throws Exception {
103103
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
104104
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port));
105105
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
@@ -111,13 +111,13 @@ public void startWhenAlreadyStartedThenFails() throws Exception {
111111
}
112112

113113
@Test
114-
public void shouldBuildButNotStartTheServer() {
114+
void shouldBuildButNotStartTheServer() {
115115
EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(this.port).build();
116116
assertPortIsFree(this.port);
117117
}
118118

119119
@Test
120-
public void shouldBuildTheServerWithCustomPort() {
120+
void shouldBuildTheServerWithCustomPort() {
121121
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
122122
.port(this.port);
123123

@@ -129,7 +129,7 @@ public void shouldBuildTheServerWithCustomPort() {
129129
}
130130

131131
@Test
132-
public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException {
132+
void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException {
133133
String tempLogFile = Files.createTempFile("ldap-log-", ".txt").toAbsolutePath().toString();
134134

135135
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")

test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import javax.naming.NamingException;
2222
import javax.naming.directory.Attributes;
2323

24-
import org.junit.After;
25-
import org.junit.Test;
24+
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.context.support.ClassPathXmlApplicationContext;
2828
import org.springframework.ldap.core.AttributesMapper;
@@ -31,19 +31,19 @@
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
3333

34-
public class TestContextSourceFactoryBeanTests {
34+
class TestContextSourceFactoryBeanTests {
3535

3636
ClassPathXmlApplicationContext ctx;
3737

38-
@After
39-
public void setup() {
38+
@AfterEach
39+
void setup() {
4040
if (this.ctx != null) {
4141
this.ctx.close();
4242
}
4343
}
4444

4545
@Test
46-
public void testServerStartup() throws Exception {
46+
void serverStartup() throws Exception {
4747
this.ctx = new ClassPathXmlApplicationContext("/applicationContext-testContextSource.xml");
4848
LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class);
4949
assertThat(ldapTemplate).isNotNull();

0 commit comments

Comments
 (0)