Skip to content

Commit ec8b79a

Browse files
authored
Use assertj for assertions (#244)
1 parent cf24ce7 commit ec8b79a

Some content is hidden

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

56 files changed

+1191
-1190
lines changed

src/test/java/io/vavr/jackson/datatype/BaseTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import io.vavr.Tuple2;
1010
import io.vavr.collection.List;
1111
import io.vavr.collection.Seq;
12-
import org.junit.jupiter.api.Assertions;
1312

1413
import java.io.ByteArrayOutputStream;
1514
import java.io.IOException;
1615
import java.io.ObjectOutputStream;
1716
import java.util.Collection;
1817
import java.util.Map;
1918

19+
import static org.assertj.core.api.Assertions.assertThat;
20+
2021
public class BaseTest {
2122

2223
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@@ -34,10 +35,10 @@ protected void verifySerialization(TypeReference<?> typeReference, List<Tuple2<?
3435
String expectedJson = testValue._2();
3536

3637
String json = writer.writeValueAsString(src);
37-
Assertions.assertEquals(expectedJson, json);
38+
assertThat(json).isEqualTo(expectedJson);
3839

3940
Object dst = mapper().readValue(json, typeReference);
40-
Assertions.assertEquals(src, dst);
41+
assertThat(dst).isEqualTo(src);
4142
}
4243
}
4344

src/test/java/io/vavr/jackson/datatype/CharSeqTest.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.databind.ObjectWriter;
66
import io.vavr.collection.CharSeq;
7-
import org.junit.jupiter.api.Assertions;
87
import org.junit.jupiter.api.Test;
98

109
import java.io.IOException;
1110

12-
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
1313

1414
class CharSeqTest extends BaseTest {
1515

@@ -18,9 +18,9 @@ void shouldSerializeAndDeserializeCharSeq() throws IOException {
1818
ObjectWriter writer = mapper().writer();
1919
CharSeq src = CharSeq.of("abc");
2020
String json = writer.writeValueAsString(src);
21-
Assertions.assertEquals("\"abc\"", json);
21+
assertThat(json).isEqualTo("\"abc\"");
2222
CharSeq dst = mapper().readValue(json, CharSeq.class);
23-
Assertions.assertEquals(src, dst);
23+
assertThat((Iterable) dst).isEqualTo(src);
2424
}
2525

2626
@Test
@@ -29,9 +29,9 @@ void shouldSerializeAndDeserializeWrappedCharSeqAsObject() throws IOException {
2929
CharSeq src = CharSeq.of("abc");
3030
String plainJson = mapper().writeValueAsString(src);
3131
String wrappedJson = mapper.writeValueAsString(src);
32-
Assertions.assertEquals(wrappedJson, wrapToObject(CharSeq.class.getName(), plainJson));
32+
assertThat(wrapToObject(CharSeq.class.getName(), plainJson)).isEqualTo(wrappedJson);
3333
CharSeq restored = mapper.readValue(wrappedJson, CharSeq.class);
34-
Assertions.assertEquals(src, restored);
34+
assertThat((Iterable) restored).isEqualTo(src);
3535
}
3636

3737
@Test
@@ -40,13 +40,13 @@ void shouldSerializeAndDeserializeWrappedCharSeqAsArray() throws IOException {
4040
CharSeq src = CharSeq.of("abc");
4141
String plainJson = mapper().writeValueAsString(src);
4242
String wrappedJson = mapper.writeValueAsString(src);
43-
Assertions.assertEquals(wrappedJson, wrapToArray(CharSeq.class.getName(), plainJson));
43+
assertThat(wrapToArray(CharSeq.class.getName(), plainJson)).isEqualTo(wrappedJson);
4444
CharSeq restored = mapper.readValue(wrappedJson, CharSeq.class);
45-
Assertions.assertEquals(src, restored);
45+
assertThat((Iterable) restored).isEqualTo(src);
4646
}
4747

4848
@Test
4949
void shouldThrowExceptionWhenDeserializingInvalidCharSeq() {
50-
assertThrows(JsonMappingException.class, () -> mapper().readValue("42", CharSeq.class));
50+
assertThatExceptionOfType(JsonMappingException.class).isThrownBy(() -> mapper().readValue("42", CharSeq.class));
5151
}
5252
}

src/test/java/io/vavr/jackson/datatype/EitherTest.java

+29-32
Original file line numberDiff line numberDiff line change
@@ -12,74 +12,71 @@
1212
import io.vavr.collection.Set;
1313
import io.vavr.control.Either;
1414
import io.vavr.control.Option;
15-
import org.junit.jupiter.api.Assertions;
1615
import org.junit.jupiter.api.Test;
1716

1817
import java.io.IOException;
1918
import java.math.BigInteger;
2019

2120
import static io.vavr.control.Option.none;
2221
import static io.vavr.control.Option.some;
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
2324

24-
public class EitherTest extends BaseTest {
25+
class EitherTest extends BaseTest {
2526

2627
@Test
2728
void shouldSerializeAndDeserializeWrappedEitherObject() throws IOException {
2829
ObjectMapper mapper = mapper().addMixIn(Either.class, WrapperObject.class);
2930
Either<Integer, Integer> src = Either.right(1);
3031
String plainJson = mapper().writeValueAsString(src);
3132
String wrappedJson = mapper.writeValueAsString(src);
32-
Assertions.assertEquals(wrappedJson, wrapToObject(Either.Right.class.getName(), plainJson));
33+
assertThat(wrapToObject(Either.Right.class.getName(), plainJson)).isEqualTo(wrappedJson);
3334
Either<Integer, Integer> restored = (Either<Integer, Integer>) mapper.readValue(wrappedJson, Either.class);
34-
Assertions.assertEquals(src, restored);
35+
assertThat(restored).isEqualTo(src);
3536
}
3637

3738
@Test
3839
void shouldSerializeAndDeserializeLeftEither() throws IOException {
3940
Either<String, Integer> left = Either.left("left");
4041
String json = mapper().writer().writeValueAsString(left);
4142
Either<String, Integer> restored = mapper().readValue(json, Either.class);
42-
Assertions.assertEquals(left, restored);
43+
assertThat(restored).isEqualTo(left);
4344
}
4445

4546
@Test
4647
void shouldSerializeAndDeserializeRightEither() throws IOException {
4748
Either<String, Integer> right = Either.right(1);
4849
String json = mapper().writer().writeValueAsString(right);
4950
Either<String, Integer> restored = mapper().readValue(json, Either.class);
50-
Assertions.assertEquals(right, restored);
51+
assertThat(restored).isEqualTo(right);
5152
}
5253

5354
@Test
5455
void shouldThrowExceptionWhenInvalidJsonHasExtraRightValue() throws IOException {
55-
Assertions.assertThrows(JsonMappingException.class, () -> {
56-
String json = "[\"right\", 2, 3]";
57-
mapper().readValue(json, Either.class);
58-
});
56+
String json = "[\"right\", 2, 3]";
57+
assertThatExceptionOfType(JsonMappingException.class).isThrownBy(() ->
58+
mapper().readValue(json, Either.class));
5959
}
6060

6161
@Test
6262
void shouldThrowExceptionWhenInvalidJsonHasMissingRightValue() throws IOException {
63-
Assertions.assertThrows(JsonMappingException.class, () -> {
64-
String json = "[\"right\"]";
65-
mapper().readValue(json, Either.class);
66-
});
63+
String json = "[\"right\"]";
64+
assertThatExceptionOfType(JsonMappingException.class).isThrownBy(() ->
65+
mapper().readValue(json, Either.class));
6766
}
6867

6968
@Test
7069
void shouldThrowExceptionWhenInvalidJsonHasMalformedLeftType() throws IOException {
71-
Assertions.assertThrows(JsonMappingException.class, () -> {
72-
String json = "[\"lEft\", 42]";
73-
mapper().readValue(json, Either.class);
74-
});
70+
String json = "[\"lEft\", 42]";
71+
assertThatExceptionOfType(JsonMappingException.class).isThrownBy(() ->
72+
mapper().readValue(json, Either.class));
7573
}
7674

7775
@Test
7876
void shouldThrowExceptionWhenInvalidJsonHasNonStringLeftOrRight() throws IOException {
79-
Assertions.assertThrows(JsonMappingException.class, () -> {
80-
String json = "[42, 42]";
81-
mapper().readValue(json, Either.class);
82-
});
77+
String json = "[42, 42]";
78+
assertThatExceptionOfType(JsonMappingException.class).isThrownBy(() ->
79+
mapper().readValue(json, Either.class));
8380
}
8481

8582
@Test
@@ -88,7 +85,7 @@ void shouldSerializeAndDeserializeLeftEitherWithGenericTypes() throws IOExceptio
8885
String json = mapper().writer().writeValueAsString(either);
8986
Either<List<Integer>, Set<Double>> restored = mapper().readValue(json, new TypeReference<Either<List<Integer>, Set<Double>>>() {
9087
});
91-
Assertions.assertEquals(either, restored);
88+
assertThat(restored).isEqualTo(either);
9289
}
9390

9491
@Test
@@ -97,23 +94,23 @@ void shouldSerializeAndDeserializeRightEitherWithGenericTypes() throws IOExcepti
9794
String json = mapper().writer().writeValueAsString(either);
9895
Either<List<Integer>, Set<Double>> restored = mapper().readValue(json, new TypeReference<Either<List<Integer>, Set<Double>>>() {
9996
});
100-
Assertions.assertEquals(either, restored);
97+
assertThat(restored).isEqualTo(either);
10198
}
10299

103100
@Test
104101
void shouldHandleNullInLeftEitherDuringSerialization() throws IOException {
105102
Either<String, Integer> left = Either.left(null);
106103
String leftJson = mapper().writer().writeValueAsString(left);
107104
Either<String, Integer> restoredLeft = mapper().readValue(leftJson, Either.class);
108-
Assertions.assertEquals(left, restoredLeft);
105+
assertThat(restoredLeft).isEqualTo(left);
109106
}
110107

111108
@Test
112109
void shouldHandleNullInRightEitherDuringSerialization() throws IOException {
113110
Either<String, Integer> right = Either.left(null);
114111
String rightJson = mapper().writer().writeValueAsString(right);
115112
Either<String, Integer> restoredRight = mapper().readValue(rightJson, Either.class);
116-
Assertions.assertEquals(right, restoredRight);
113+
assertThat(restoredRight).isEqualTo(right);
117114
}
118115

119116
@Test
@@ -132,13 +129,13 @@ void shouldSerializeAndDeserializeEitherWithOption() throws IOException {
132129
void shouldSerializeAndDeserializeCustomTypeWithJsonTypeInfo() throws IOException {
133130
String javaUtilValue;
134131
javaUtilValue = mapper().writeValueAsString(new Left());
135-
Assertions.assertEquals("{\"f\":[\"left\",{\"card\":{\"type\":\"hello\"}}]}", javaUtilValue);
132+
assertThat(javaUtilValue).isEqualTo("{\"f\":[\"left\",{\"card\":{\"type\":\"hello\"}}]}");
136133
Left restoredLeft = mapper().readValue(javaUtilValue, Left.class);
137-
Assertions.assertEquals("hello", restoredLeft.f.getLeft().type);
134+
assertThat(restoredLeft.f.getLeft().type).isEqualTo("hello");
138135
javaUtilValue = mapper().writeValueAsString(new Right());
139-
Assertions.assertEquals("{\"f\":[\"right\",{\"card\":{\"type\":\"hello\"}}]}", javaUtilValue);
136+
assertThat(javaUtilValue).isEqualTo("{\"f\":[\"right\",{\"card\":{\"type\":\"hello\"}}]}");
140137
Right restoredRight = mapper().readValue(javaUtilValue, Right.class);
141-
Assertions.assertEquals("hello", restoredRight.f.get().type);
138+
assertThat(restoredRight.f.get().type).isEqualTo("hello");
142139
}
143140

144141
@Test
@@ -151,13 +148,13 @@ void shouldSerializeAndDeserializeScalaEither() throws IOException {
151148
final Either<String, BigInteger> deserializedLeft =
152149
mapper.readValue(serializedLeft, new TypeReference<Either<String, BigInteger>>() {
153150
});
154-
Assertions.assertEquals("test", deserializedLeft.getLeft());
151+
assertThat(deserializedLeft.getLeft()).isEqualTo("test");
155152

156153
final String serializedRight = mapper.writeValueAsString(right);
157154
final Either<String, BigInteger> deserializedRight =
158155
mapper.readValue(serializedRight, new TypeReference<Either<String, BigInteger>>() {
159156
});
160-
Assertions.assertEquals(BigInteger.ONE, deserializedRight.get());
157+
assertThat(deserializedRight.get()).isEqualTo(BigInteger.ONE);
161158
}
162159

163160
@JsonTypeInfo(

0 commit comments

Comments
 (0)