1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .util .Optional ;
20
20
21
+ import org .junit .jupiter .api .BeforeEach ;
21
22
import org .junit .jupiter .api .Test ;
22
23
23
24
import org .springframework .core .convert .ConverterNotFoundException ;
29
30
* Unit tests for {@link ObjectToObjectConverter}.
30
31
*
31
32
* @author Sam Brannen
32
- * @author Phil Webb
33
+ * @author Phillip Webb
33
34
* @since 5.3.21
34
35
* @see org.springframework.core.convert.converter.DefaultConversionServiceTests#convertObjectToObjectUsingValueOfMethod()
35
36
*/
36
37
class ObjectToObjectConverterTests {
37
38
38
- private final GenericConversionService conversionService = new GenericConversionService () {{
39
- addConverter (new ObjectToObjectConverter ());
40
- }};
39
+ private final GenericConversionService conversionService = new GenericConversionService ();
40
+
41
+
42
+ @ BeforeEach
43
+ void setup () {
44
+ conversionService .addConverter (new ObjectToObjectConverter ());
45
+ }
41
46
42
47
43
48
/**
@@ -47,29 +52,29 @@ class ObjectToObjectConverterTests {
47
52
@ Test
48
53
void nonStaticToTargetTypeSimpleNameMethodWithMatchingReturnType () {
49
54
assertThat (conversionService .canConvert (Source .class , Data .class ))
50
- .as ("can convert Source to Data" ).isTrue ();
55
+ .as ("can convert Source to Data" ).isTrue ();
51
56
Data data = conversionService .convert (new Source ("test" ), Data .class );
52
57
assertThat (data ).asString ().isEqualTo ("test" );
53
58
}
54
59
55
60
@ Test
56
61
void nonStaticToTargetTypeSimpleNameMethodWithDifferentReturnType () {
57
62
assertThat (conversionService .canConvert (Text .class , Data .class ))
58
- .as ("can convert Text to Data" ).isFalse ();
63
+ .as ("can convert Text to Data" ).isFalse ();
59
64
assertThat (conversionService .canConvert (Text .class , Optional .class ))
60
- .as ("can convert Text to Optional" ).isFalse ();
65
+ .as ("can convert Text to Optional" ).isFalse ();
61
66
assertThatExceptionOfType (ConverterNotFoundException .class )
62
- .as ("convert Text to Data" )
63
- .isThrownBy (() -> conversionService .convert (new Text ("test" ), Data .class ));
67
+ .as ("convert Text to Data" )
68
+ .isThrownBy (() -> conversionService .convert (new Text ("test" ), Data .class ));
64
69
}
65
70
66
71
@ Test
67
72
void staticValueOfFactoryMethodWithDifferentReturnType () {
68
73
assertThat (conversionService .canConvert (String .class , Data .class ))
69
- .as ("can convert String to Data" ).isFalse ();
74
+ .as ("can convert String to Data" ).isFalse ();
70
75
assertThatExceptionOfType (ConverterNotFoundException .class )
71
- .as ("convert String to Data" )
72
- .isThrownBy (() -> conversionService .convert ("test" , Data .class ));
76
+ .as ("convert String to Data" )
77
+ .isThrownBy (() -> conversionService .convert ("test" , Data .class ));
73
78
}
74
79
75
80
@@ -84,9 +89,9 @@ private Source(String value) {
84
89
public Data toData () {
85
90
return new Data (this .value );
86
91
}
87
-
88
92
}
89
93
94
+
90
95
static class Text {
91
96
92
97
private final String value ;
@@ -98,9 +103,9 @@ private Text(String value) {
98
103
public Optional <Data > toData () {
99
104
return Optional .of (new Data (this .value ));
100
105
}
101
-
102
106
}
103
107
108
+
104
109
static class Data {
105
110
106
111
private final String value ;
@@ -115,9 +120,8 @@ public String toString() {
115
120
}
116
121
117
122
public static Optional <Data > valueOf (String string ) {
118
- return (string != null ) ? Optional .of (new Data (string )) : Optional .empty ();
123
+ return (string != null ? Optional .of (new Data (string )) : Optional .empty () );
119
124
}
120
-
121
125
}
122
126
123
127
}
0 commit comments