31
31
32
32
import static org .assertj .core .api .Assertions .assertThat ;
33
33
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
34
- import static org .assertj .core .api .Assertions .assertThatThrownBy ;
34
+ import static org .assertj .core .api .Assertions .assertThatIOException ;
35
35
import static org .mockito .BDDMockito .given ;
36
36
37
-
38
37
/**
38
+ * Unit tests for {@link SerializingConverter} and {@link DeserializingConverter}.
39
+ *
39
40
* @author Gary Russell
40
41
* @author Mark Fisher
41
42
* @since 3.0.5
42
43
*/
43
44
class SerializationConverterTests {
44
45
45
46
@ Test
46
- void serializeAndDeserializeString () {
47
+ void serializeAndDeserializeStringWithDefaultSerializer () {
47
48
SerializingConverter toBytes = new SerializingConverter ();
48
49
byte [] bytes = toBytes .convert ("Testing" );
49
50
DeserializingConverter fromBytes = new DeserializingConverter ();
50
51
assertThat (fromBytes .convert (bytes )).isEqualTo ("Testing" );
51
52
}
52
53
53
54
@ Test
54
- void serializeAndDeserializeStringWithCustomSerializer () {
55
+ void serializeAndDeserializeStringWithExplicitSerializer () {
55
56
SerializingConverter toBytes = new SerializingConverter (new DefaultSerializer ());
56
57
byte [] bytes = toBytes .convert ("Testing" );
57
58
DeserializingConverter fromBytes = new DeserializingConverter ();
@@ -63,7 +64,9 @@ void nonSerializableObject() {
63
64
SerializingConverter toBytes = new SerializingConverter ();
64
65
assertThatExceptionOfType (SerializationFailedException .class )
65
66
.isThrownBy (() -> toBytes .convert (new Object ()))
66
- .withCauseInstanceOf (IllegalArgumentException .class );
67
+ .havingCause ()
68
+ .isInstanceOf (IllegalArgumentException .class )
69
+ .withMessageContaining ("requires a Serializable payload" );
67
70
}
68
71
69
72
@ Test
@@ -82,15 +85,15 @@ void deserializationFailure() {
82
85
}
83
86
84
87
@ Test
85
- void deserializationWithClassLoader () {
86
- DeserializingConverter fromBytes = new DeserializingConverter (this . getClass ().getClassLoader ());
88
+ void deserializationWithExplicitClassLoader () {
89
+ DeserializingConverter fromBytes = new DeserializingConverter (getClass ().getClassLoader ());
87
90
SerializingConverter toBytes = new SerializingConverter ();
88
91
String expected = "SPRING FRAMEWORK" ;
89
92
assertThat (fromBytes .convert (toBytes .convert (expected ))).isEqualTo (expected );
90
93
}
91
94
92
95
@ Test
93
- void deserializationWithDeserializer () {
96
+ void deserializationWithExplicitDeserializer () {
94
97
DeserializingConverter fromBytes = new DeserializingConverter (new DefaultDeserializer ());
95
98
SerializingConverter toBytes = new SerializingConverter ();
96
99
String expected = "SPRING FRAMEWORK" ;
@@ -99,24 +102,26 @@ void deserializationWithDeserializer() {
99
102
100
103
@ Test
101
104
void deserializationIOException () {
102
- try (MockedConstruction <ConfigurableObjectInputStream > mocked = Mockito .mockConstruction (
103
- ConfigurableObjectInputStream .class , (mock , context ) -> given (mock .readObject ())
104
- .willThrow (new ClassNotFoundException ()))) {
105
- DefaultDeserializer defaultSerializer = new DefaultDeserializer (this .getClass ().getClassLoader ());
105
+ ClassNotFoundException classNotFoundException = new ClassNotFoundException ();
106
+ try (MockedConstruction <ConfigurableObjectInputStream > mocked =
107
+ Mockito .mockConstruction (ConfigurableObjectInputStream .class ,
108
+ (mock , context ) -> given (mock .readObject ()).willThrow (classNotFoundException ))) {
109
+ DefaultDeserializer defaultSerializer = new DefaultDeserializer (getClass ().getClassLoader ());
106
110
assertThat (mocked ).isNotNull ();
107
- assertThatThrownBy (() -> defaultSerializer .deserialize (
108
- new ByteArrayInputStream ("test" .getBytes ())))
109
- .hasMessage ("Failed to deserialize object type" );
111
+ assertThatIOException ()
112
+ .isThrownBy (() -> defaultSerializer .deserialize (new ByteArrayInputStream ("test" .getBytes ())))
113
+ .withMessage ("Failed to deserialize object type" )
114
+ .havingCause ().isSameAs (classNotFoundException );
110
115
}
111
116
}
112
117
113
118
114
- class UnSerializable implements Serializable {
119
+ static class UnSerializable implements Serializable {
115
120
116
121
private static final long serialVersionUID = 1L ;
117
122
118
123
@ SuppressWarnings ({"unused" , "serial" })
119
- private Object object ;
124
+ private Object object = new Object () ;
120
125
}
121
126
122
127
}
0 commit comments