Skip to content

Commit 96a1a0d

Browse files
committed
Convert InstantFormatterTests to JUnit 4
See gh-23895
1 parent 40ac055 commit 96a1a0d

File tree

1 file changed

+53
-57
lines changed

1 file changed

+53
-57
lines changed

spring-context/src/test/java/org/springframework/format/datetime/standard/InstantFormatterTests.java

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,12 @@
2222
import java.util.Random;
2323
import java.util.stream.Stream;
2424

25-
import org.junit.jupiter.api.DisplayName;
26-
import org.junit.jupiter.api.DisplayNameGeneration;
27-
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
28-
import org.junit.jupiter.api.extension.ExtensionContext;
29-
import org.junit.jupiter.params.ParameterizedTest;
30-
import org.junit.jupiter.params.provider.Arguments;
31-
import org.junit.jupiter.params.provider.ArgumentsProvider;
32-
import org.junit.jupiter.params.provider.ArgumentsSource;
25+
import org.junit.Test;
3326

3427
import static java.time.Instant.MAX;
3528
import static java.time.Instant.MIN;
3629
import static java.time.ZoneId.systemDefault;
37-
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.junit.Assert.assertEquals;
3831

3932
/**
4033
* Unit tests for {@link InstantFormatter}.
@@ -43,83 +36,86 @@
4336
* @author Sam Brannen
4437
* @since 5.1.12
4538
*/
46-
@DisplayName("InstantFormatter unit tests")
47-
@DisplayNameGeneration(ReplaceUnderscores.class)
48-
class InstantFormatterTests {
39+
public class InstantFormatterTests {
4940

5041
private final InstantFormatter instantFormatter = new InstantFormatter();
5142

52-
@ParameterizedTest
53-
@ArgumentsSource(ISOSerializedInstantProvider.class)
54-
void should_parse_an_ISO_formatted_string_representation_of_an_Instant(String input) throws ParseException {
55-
Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from);
5643

57-
Instant actual = instantFormatter.parse(input, null);
44+
@Test
45+
public void should_parse_an_ISO_formatted_string_representation_of_an_Instant() {
46+
new ISOSerializedInstantProvider().provideArguments().forEach(input -> {
47+
try {
48+
Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from);
5849

59-
assertThat(actual).isEqualTo(expected);
50+
Instant actual = instantFormatter.parse(input, null);
51+
52+
assertEquals(expected, actual);
53+
}
54+
catch (ParseException ex) {
55+
throw new RuntimeException(ex);
56+
}
57+
});
6058
}
6159

62-
@ParameterizedTest
63-
@ArgumentsSource(RFC1123SerializedInstantProvider.class)
64-
void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant(String input) throws ParseException {
65-
Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from);
60+
@Test
61+
public void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant() {
62+
new RFC1123SerializedInstantProvider().provideArguments().forEach(input -> {
63+
try {
64+
Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from);
6665

67-
Instant actual = instantFormatter.parse(input, null);
66+
Instant actual = instantFormatter.parse(input, null);
6867

69-
assertThat(actual).isEqualTo(expected);
68+
assertEquals(expected, actual);
69+
}
70+
catch (ParseException ex) {
71+
throw new RuntimeException(ex);
72+
}
73+
});
7074
}
7175

72-
@ParameterizedTest
73-
@ArgumentsSource(RandomInstantProvider.class)
74-
void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale(Instant input) {
75-
String expected = DateTimeFormatter.ISO_INSTANT.format(input);
76+
@Test
77+
public void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale() {
78+
new RandomInstantProvider().randomInstantStream(MIN, MAX).forEach(instant -> {
79+
String expected = DateTimeFormatter.ISO_INSTANT.format(instant);
80+
81+
String actual = instantFormatter.print(instant, null);
82+
83+
assertEquals(expected, actual);
84+
});
85+
}
86+
87+
88+
private static class RandomInstantProvider {
89+
90+
private static final long DATA_SET_SIZE = 10;
7691

77-
String actual = instantFormatter.print(input, null);
92+
private static final Random random = new Random();
7893

79-
assertThat(actual).isEqualTo(expected);
94+
Stream<Instant> randomInstantStream(Instant min, Instant max) {
95+
return Stream.concat(Stream.of(Instant.now()), // make sure that the data set includes current instant
96+
random.longs(min.getEpochSecond(), max.getEpochSecond()).limit(DATA_SET_SIZE).mapToObj(Instant::ofEpochSecond));
97+
}
8098
}
8199

82100
private static class ISOSerializedInstantProvider extends RandomInstantProvider {
83101

84-
@Override
85-
Stream<?> provideArguments() {
102+
Stream<String> provideArguments() {
86103
return randomInstantStream(MIN, MAX).map(DateTimeFormatter.ISO_INSTANT::format);
87104
}
88105
}
89106

90107
private static class RFC1123SerializedInstantProvider extends RandomInstantProvider {
91108

92109
// RFC-1123 supports only 4-digit years
93-
private final Instant min = Instant.parse("0000-01-01T00:00:00.00Z");
110+
private static final Instant min = Instant.parse("0000-01-01T00:00:00.00Z");
111+
112+
private static final Instant max = Instant.parse("9999-12-31T23:59:59.99Z");
94113

95-
private final Instant max = Instant.parse("9999-12-31T23:59:59.99Z");
96114

97-
@Override
98-
Stream<?> provideArguments() {
115+
Stream<String> provideArguments() {
99116
return randomInstantStream(min, max)
100117
.map(DateTimeFormatter.RFC_1123_DATE_TIME.withZone(systemDefault())::format);
101118
}
102119
}
103120

104-
private static class RandomInstantProvider implements ArgumentsProvider {
105-
106-
private static final long DATA_SET_SIZE = 10;
107-
108-
private static final Random random = new Random();
109-
110-
Stream<?> provideArguments() {
111-
return randomInstantStream(MIN, MAX);
112-
}
113-
114-
@Override
115-
public final Stream<? extends Arguments> provideArguments(ExtensionContext context) {
116-
return provideArguments().map(Arguments::of).limit(DATA_SET_SIZE);
117-
}
118-
119-
Stream<Instant> randomInstantStream(Instant min, Instant max) {
120-
return Stream.concat(Stream.of(Instant.now()), // make sure that the data set includes current instant
121-
random.longs(min.getEpochSecond(), max.getEpochSecond()).mapToObj(Instant::ofEpochSecond));
122-
}
123-
}
124-
125121
}

0 commit comments

Comments
 (0)