Skip to content

Commit 1e1f354

Browse files
author
bnasslahsen
committed
Upgrade Tests to Junit 5
1 parent c78dd1b commit 1e1f354

File tree

50 files changed

+283
-458
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+283
-458
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@
114114
<groupId>org.springframework.boot</groupId>
115115
<artifactId>spring-boot-starter-test</artifactId>
116116
<scope>test</scope>
117+
<exclusions>
118+
<exclusion>
119+
<groupId>org.junit.vintage</groupId>
120+
<artifactId>junit-vintage-engine</artifactId>
121+
</exclusion>
122+
</exclusions>
117123
</dependency>
118124
</dependencies>
119125
<build>

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/AbstractSpringDocTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package test.org.springdoc.api;
22

3-
import org.junit.Test;
4-
import org.junit.runner.RunWith;
3+
import nonapi.io.github.classgraph.utils.FileUtils;
4+
import org.junit.jupiter.api.Test;
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77
import org.springdoc.core.Constants;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1010
import org.springframework.boot.test.context.SpringBootTest;
1111
import org.springframework.test.context.ActiveProfiles;
12-
import org.springframework.test.context.junit4.SpringRunner;
1312
import org.springframework.test.web.servlet.MockMvc;
1413
import org.springframework.test.web.servlet.MvcResult;
1514

15+
import java.nio.charset.StandardCharsets;
1616
import java.nio.file.Files;
1717
import java.nio.file.Path;
1818
import java.nio.file.Paths;
@@ -23,9 +23,8 @@
2323
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2424
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2525

26-
@RunWith(SpringRunner.class)
27-
@SpringBootTest
2826
@ActiveProfiles("test")
27+
@SpringBootTest
2928
@AutoConfigureMockMvc
3029
public abstract class AbstractSpringDocTest {
3130

@@ -49,4 +48,13 @@ public void testApp() throws Exception {
4948
assertEquals(expected, result, true);
5049
}
5150

51+
public static String getContent(String fileName) throws Exception {
52+
try {
53+
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
54+
byte[] fileBytes = Files.readAllBytes(path);
55+
return new String(fileBytes, StandardCharsets.UTF_8);
56+
} catch (Exception e) {
57+
throw new RuntimeException("Failed to read file: " + fileName, e);
58+
}
59+
}
5260
}
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
package test.org.springdoc.api;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import org.junit.Test;
5-
import org.junit.runner.RunWith;
3+
import nonapi.io.github.classgraph.utils.FileUtils;
4+
import org.junit.jupiter.api.Test;
5+
import org.skyscreamer.jsonassert.JSONAssert;
66
import org.springdoc.core.Constants;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
99
import org.springframework.test.context.ActiveProfiles;
10-
import org.springframework.test.context.junit4.SpringRunner;
1110
import org.springframework.test.web.reactive.server.EntityExchangeResult;
1211
import org.springframework.test.web.reactive.server.WebTestClient;
1312

13+
import java.nio.charset.StandardCharsets;
1414
import java.nio.file.Files;
1515
import java.nio.file.Path;
1616
import java.nio.file.Paths;
1717

18-
import static org.junit.Assert.assertEquals;
19-
20-
@RunWith(SpringRunner.class)
2118
@WebFluxTest
2219
@ActiveProfiles("test")
2320
public abstract class AbstractSpringDocTest {
2421

2522
@Autowired
2623
private WebTestClient webTestClient;
2724

28-
@Autowired
29-
private ObjectMapper objectMapper;
3025

3126
@Test
3227
public void testApp() throws Exception {
@@ -37,11 +32,17 @@ public void testApp() throws Exception {
3732
String className = getClass().getSimpleName();
3833
String testNumber = className.replaceAll("[^0-9]", "");
3934

40-
Path path = Paths.get(getClass().getClassLoader().getResource("results/app" + testNumber + ".json").toURI());
41-
byte[] fileBytes = Files.readAllBytes(path);
42-
String expected = new String(fileBytes);
43-
44-
assertEquals(objectMapper.readTree(expected), objectMapper.readTree(result));
35+
String expected = getContent("results/app" + testNumber + ".json");
36+
JSONAssert.assertEquals(expected, result, true);
4537
}
4638

47-
}
39+
public static String getContent(String fileName) throws Exception {
40+
try {
41+
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
42+
byte[] fileBytes = Files.readAllBytes(path);
43+
return new String(fileBytes, StandardCharsets.UTF_8);
44+
} catch (Exception e) {
45+
throw new RuntimeException("Failed to read file: " + fileName, e);
46+
}
47+
}
48+
}
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
package test.org.springdoc.api
22

3-
import com.fasterxml.jackson.databind.ObjectMapper
4-
import org.junit.Assert.assertEquals
5-
import org.junit.Test
6-
import org.junit.runner.RunWith
3+
import nonapi.io.github.classgraph.utils.FileUtils
4+
import org.junit.jupiter.api.Test
5+
import org.skyscreamer.jsonassert.JSONAssert
76
import org.springdoc.core.Constants
87
import org.springframework.beans.factory.annotation.Autowired
98
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
109
import org.springframework.test.context.ActiveProfiles
11-
import org.springframework.test.context.junit4.SpringRunner
1210
import org.springframework.test.web.reactive.server.WebTestClient
11+
import java.nio.charset.StandardCharsets
1312
import java.nio.file.Files
1413
import java.nio.file.Paths
15-
16-
@RunWith(SpringRunner::class)
1714
@WebFluxTest
1815
@ActiveProfiles("test")
1916
abstract class AbstractKotlinSpringDocTest {
2017

2118
@Autowired
2219
private val webTestClient: WebTestClient? = null
2320

24-
@Autowired
25-
private val objectMapper: ObjectMapper? = null
2621

2722
@Test
2823
@Throws(Exception::class)
@@ -34,11 +29,22 @@ abstract class AbstractKotlinSpringDocTest {
3429
val className = javaClass.simpleName
3530
val testNumber = className.replace("[^0-9]".toRegex(), "")
3631

37-
val path = Paths.get(javaClass.classLoader.getResource("results/app$testNumber.json")!!.toURI())
38-
val fileBytes = Files.readAllBytes(path)
39-
val expected = String(fileBytes)
40-
41-
assertEquals(objectMapper!!.readTree(expected), objectMapper.readTree(result))
32+
val expected = getContent("results/app$testNumber.json")
33+
JSONAssert.assertEquals(expected, result, true)
4234
}
4335

44-
}
36+
companion object {
37+
38+
@Throws(Exception::class)
39+
fun getContent(fileName: String): String {
40+
try {
41+
val path = Paths.get(FileUtils::class.java.classLoader.getResource(fileName)!!.toURI())
42+
val fileBytes = Files.readAllBytes(path)
43+
return String(fileBytes, StandardCharsets.UTF_8)
44+
} catch (e: Exception) {
45+
throw RuntimeException("Failed to read file: $fileName", e)
46+
}
47+
48+
}
49+
}
50+
}
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package test.org.springdoc.api;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import org.junit.Test;
5-
import org.junit.runner.RunWith;
6-
import org.skyscreamer.jsonassert.JSONAssert;
3+
import nonapi.io.github.classgraph.utils.FileUtils;
4+
import org.junit.jupiter.api.Test;
75
import org.springdoc.core.Constants;
86
import org.springframework.beans.factory.annotation.Autowired;
97
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -12,22 +10,22 @@
1210
import org.springframework.boot.test.context.SpringBootTest;
1311
import org.springframework.context.annotation.Configuration;
1412
import org.springframework.test.context.ActiveProfiles;
15-
import org.springframework.test.context.junit4.SpringRunner;
1613
import org.springframework.test.web.servlet.MockMvc;
1714
import org.springframework.test.web.servlet.MvcResult;
1815

16+
import java.nio.charset.StandardCharsets;
1917
import java.nio.file.Files;
2018
import java.nio.file.Path;
2119
import java.nio.file.Paths;
2220

2321
import static org.hamcrest.Matchers.is;
22+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
2423
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2524
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2625
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2726

28-
@RunWith(SpringRunner.class)
29-
@SpringBootTest
3027
@ActiveProfiles("test")
28+
@SpringBootTest
3129
@AutoConfigureMockMvc
3230
public abstract class AbstractSpringDocTest {
3331

@@ -36,9 +34,6 @@ public abstract class AbstractSpringDocTest {
3634
@Autowired
3735
protected MockMvc mockMvc;
3836

39-
@Autowired
40-
private ObjectMapper objectMapper;
41-
4237
@Configuration
4338
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class})
4439
static class ContextConfiguration { }
@@ -50,11 +45,18 @@ public void testApp() throws Exception {
5045
MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk())
5146
.andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn();
5247
String result = mockMvcResult.getResponse().getContentAsString();
53-
Path path = Paths.get(getClass().getClassLoader().getResource("results/app" + testNumber + ".json").toURI());
54-
byte[] fileBytes = Files.readAllBytes(path);
55-
String expected = new String(fileBytes);
56-
JSONAssert.assertEquals(expected, result, true);
48+
String expected = getContent("results/app" + testNumber + ".json");
49+
assertEquals(expected, result, true);
5750
}
5851

5952

53+
public static String getContent(String fileName) throws Exception {
54+
try {
55+
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
56+
byte[] fileBytes = Files.readAllBytes(path);
57+
return new String(fileBytes, StandardCharsets.UTF_8);
58+
} catch (Exception e) {
59+
throw new RuntimeException("Failed to read file: " + fileName, e);
60+
}
61+
}
6062
}

springdoc-openapi-security/src/test/java/test/org/springdoc/api/app3/SpringOauth2Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package test.org.springdoc.api.app3;
22

3-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.SpringBootConfiguration;
55
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
66

springdoc-openapi-security/src/test/java/test/org/springdoc/api/app4/SpringDocApp4Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package test.org.springdoc.api.app4;
22

3-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
66
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package test.org.springdoc.ui;
22

3-
import org.junit.runner.RunWith;
3+
import nonapi.io.github.classgraph.utils.FileUtils;
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
66
import org.springframework.boot.test.context.SpringBootTest;
77
import org.springframework.test.context.ActiveProfiles;
8-
import org.springframework.test.context.junit4.SpringRunner;
98
import org.springframework.test.web.servlet.MockMvc;
109

10+
import java.nio.charset.StandardCharsets;
1111
import java.nio.file.Files;
1212
import java.nio.file.Path;
1313
import java.nio.file.Paths;
1414

15-
@RunWith(SpringRunner.class)
16-
@SpringBootTest
1715
@ActiveProfiles("test")
16+
@SpringBootTest
1817
@AutoConfigureMockMvc
1918
public abstract class AbstractSpringDocTest {
2019

@@ -26,8 +25,16 @@ public abstract class AbstractSpringDocTest {
2625
protected String getExpectedResult() throws Exception {
2726
className = getClass().getSimpleName();
2827
String testNumber = className.replaceAll("[^0-9]", "");
29-
Path path = Paths.get(getClass().getClassLoader().getResource("results/app" + testNumber).toURI());
30-
byte[] fileBytes = Files.readAllBytes(path);
31-
return new String(fileBytes);
28+
return getContent("results/app" + testNumber );
29+
}
30+
31+
public static String getContent(String fileName) throws Exception {
32+
try {
33+
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
34+
byte[] fileBytes = Files.readAllBytes(path);
35+
return new String(fileBytes, StandardCharsets.UTF_8);
36+
} catch (Exception e) {
37+
throw new RuntimeException("Failed to read file: " + fileName, e);
38+
}
3239
}
3340
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocApp1Test.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package test.org.springdoc.ui.app1;
22

3-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.test.web.servlet.MvcResult;
66
import test.org.springdoc.ui.AbstractSpringDocTest;
77

8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertTrue;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
1010
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1111
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1212

13+
1314
public class SpringDocApp1Test extends AbstractSpringDocTest {
1415

1516
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocConfigPathsTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package test.org.springdoc.ui.app1;
22

3-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.test.context.TestPropertySource;
66
import org.springframework.test.web.servlet.MvcResult;
77
import test.org.springdoc.ui.AbstractSpringDocTest;
88

9-
import static org.junit.Assert.assertTrue;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
1010
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1111
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1212

13-
@SpringBootTest(properties = {
14-
"springdoc.swagger-ui.path=/test/swagger.html"
15-
})
13+
@TestPropertySource(properties = "springdoc.swagger-ui.path=/test/swagger.html")
1614
public class SpringDocConfigPathsTest extends AbstractSpringDocTest {
1715

1816
@SpringBootApplication

0 commit comments

Comments
 (0)