Skip to content

Commit c6a6db3

Browse files
[BAEL-9374] A Guide to @ClassTemplate in JUnit 5
- implement class template that runs date-formatting tests under multiple locales
1 parent 134a674 commit c6a6db3

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.classtemplate.extended;
2+
3+
import java.time.LocalDate;
4+
import java.time.format.DateTimeFormatter;
5+
import java.time.format.FormatStyle;
6+
import java.util.Locale;
7+
8+
class DateFormatter {
9+
10+
public String format(LocalDate date) {
11+
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
12+
.withLocale(Locale.getDefault());
13+
14+
return date.format(formatter);
15+
}
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.classtemplate.extended;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.time.LocalDate;
6+
import java.time.format.DateTimeFormatter;
7+
import java.time.format.FormatStyle;
8+
import java.util.Locale;
9+
10+
import org.junit.jupiter.api.ClassTemplate;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.extension.ExtendWith;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
@ClassTemplate
17+
@ExtendWith(DateLocaleClassTemplateProvider.class)
18+
class DateFormatterLocaleUnitTest {
19+
20+
private static final Logger LOG = LoggerFactory.getLogger(DateFormatterLocaleUnitTest.class);
21+
22+
private final DateFormatter formatter = new DateFormatter();
23+
24+
@Test
25+
void givenDefaultLocale_whenFormattingDate_thenMatchesLocalizedOutput() {
26+
LocalDate date = LocalDate.of(2025, 9, 30);
27+
28+
DateTimeFormatter expectedFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
29+
.withLocale(Locale.getDefault());
30+
31+
String expected = date.format(expectedFormatter);
32+
String formatted = formatter.format(date);
33+
34+
LOG.info("Locale: {}, Expected: {}, Formatted: {}", Locale.getDefault(), expected, formatted);
35+
36+
assertEquals(expected, formatted);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.classtemplate.extended;
2+
3+
import java.util.List;
4+
import java.util.Locale;
5+
import java.util.stream.Stream;
6+
7+
import org.junit.jupiter.api.extension.ClassTemplateInvocationContext;
8+
import org.junit.jupiter.api.extension.ClassTemplateInvocationContextProvider;
9+
import org.junit.jupiter.api.extension.Extension;
10+
import org.junit.jupiter.api.extension.ExtensionContext;
11+
12+
class DateLocaleClassTemplateProvider implements ClassTemplateInvocationContextProvider {
13+
14+
@Override
15+
public boolean supportsClassTemplate(ExtensionContext context) {
16+
return true;
17+
}
18+
19+
@Override
20+
public Stream<ClassTemplateInvocationContext> provideClassTemplateInvocationContexts(ExtensionContext context) {
21+
return Stream.of(invocationContext(Locale.US), invocationContext(Locale.GERMANY), invocationContext(Locale.ITALY), invocationContext(Locale.JAPAN));
22+
}
23+
24+
private ClassTemplateInvocationContext invocationContext(Locale locale) {
25+
return new ClassTemplateInvocationContext() {
26+
27+
@Override
28+
public String getDisplayName(int invocationIndex) {
29+
return "Locale: " + locale.getDisplayName();
30+
}
31+
32+
@Override
33+
public List<Extension> getAdditionalExtensions() {
34+
return List.of(new LocaleExtension(locale));
35+
}
36+
};
37+
}
38+
}
39+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.classtemplate.extended;
2+
3+
import org.junit.jupiter.api.extension.BeforeEachCallback;
4+
import org.junit.jupiter.api.extension.AfterEachCallback;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
7+
import java.util.Locale;
8+
9+
class LocaleExtension implements BeforeEachCallback, AfterEachCallback {
10+
11+
private final Locale locale;
12+
private Locale previous;
13+
14+
public LocaleExtension(Locale locale) {
15+
this.locale = locale;
16+
}
17+
18+
@Override
19+
public void beforeEach(ExtensionContext context) {
20+
previous = Locale.getDefault();
21+
Locale.setDefault(locale);
22+
}
23+
24+
@Override
25+
public void afterEach(ExtensionContext context) {
26+
Locale.setDefault(previous);
27+
}
28+
}
29+

0 commit comments

Comments
 (0)