Skip to content

Commit ca438ab

Browse files
committed
test: Use Reflection to test private mergeArrays method in MethodAttributes
1 parent 2ad1ce0 commit ca438ab

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/models/MethodAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private void fillMethods(String[] produces, String[] consumes, String[] headers)
305305
* @param array2 the array2
306306
* @return the string [ ]
307307
*/
308-
public String[] mergeArrays(@Nullable String[] array1, String[] array2) {
308+
private String[] mergeArrays(@Nullable String[] array1, String[] array2) {
309309
Set<String> uniqueValues = array1 == null ? new LinkedHashSet<>() : Arrays.stream(array1).collect(Collectors.toCollection(LinkedHashSet::new));
310310
uniqueValues.addAll(Arrays.asList(array2));
311311
return uniqueValues.toArray(new String[0]);

springdoc-openapi-starter-common/src/test/java/org/springdoc/core/model/MethodAttributesTest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,33 @@
1111
public class MethodAttributesTest {
1212

1313
@Test
14-
public void testMergeArrays() {
14+
public void testMergeArrays() throws Exception {
1515
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
1616

1717
String[] array1 = {"application/json", "application/xml"};
1818
String[] array2 = {"application/xml", "application/yaml"};
1919

2020
String[] expected = {"application/json", "application/xml", "application/yaml"};
21-
String[] result = methodAttributes.mergeArrays(array1, array2);
21+
22+
Method mergeArraysMethod = MethodAttributes.class.getDeclaredMethod("mergeArrays", String[].class, String[].class);
23+
mergeArraysMethod.setAccessible(true);
24+
String[] result = (String[]) mergeArraysMethod.invoke(methodAttributes, (Object) array1, (Object) array2);
2225

2326
assertArrayEquals(expected, result);
2427
}
2528

2629
@Test
27-
public void testMergeArraysWithNullArray1() {
30+
public void testMergeArraysWithNullArray1() throws Exception {
2831
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
2932

3033
String[] array1 = null;
3134
String[] array2 = {"application/xml", "application/yaml"};
3235

3336
String[] expected = {"application/xml", "application/yaml"};
34-
String[] result = methodAttributes.mergeArrays(array1, array2);
37+
38+
Method mergeArraysMethod = MethodAttributes.class.getDeclaredMethod("mergeArrays", String[].class, String[].class);
39+
mergeArraysMethod.setAccessible(true);
40+
String[] result = (String[]) mergeArraysMethod.invoke(methodAttributes, (Object) array1, (Object) array2);
3541

3642
assertArrayEquals(expected, result);
3743
}
@@ -61,4 +67,4 @@ public void testDefaultConsumesMediaType() {
6167

6268
assertArrayEquals(expectedConsumes, resultConsumes);
6369
}
64-
}
70+
}

0 commit comments

Comments
 (0)