@SpringConversion
@SpringConversion is an annotation that converts instances using the
type conversion
provided by the Spring Framework:
import io.github.scordio.junit.converters.SpringConversion ;
import org.junit.jupiter.params.ParameterizedTest ;
import org.junit.jupiter.params.provider.ValueSource ;
@ParameterizedTest
@ValueSource ( strings = "123, 456" )
void test ( @SpringConversion List < Integer > ints ) {
assertEquals ( List . of ( 123 , 456 ), ints );
}
The Spring conversion service provides a wide range of built-in converters for common Java types, including:
Primitives and their wrappers
Arrays
Collections (List, Set, Map, etc.)
Enums
Common value types (UUID, Currency, Locale, etc.)
In addition, the Spring
format annotations
are also supported:
@NumberFormat for formatting Number values such as Double and Long
@DurationFormat for formatting java.time.Duration values in ISO-8601 and simplified styles
@DateTimeFormat for formatting values such as java.util.Date, java.util.Calendar, and Long (for millisecond
timestamps) as well as JSR-310 java.time types
Requirements
The annotation requires spring-core available in the test classpath:
However, declaring this dependency is generally not required in a Spring application.
To use the Spring format annotations, spring-context must also be available:
Examples
The following sections demonstrate some of the possible conversions. For a complete list of supported conversions,
refer to the Spring Framework reference documentation.
Array → Array
Source Type
Target Declaration
Example
String []
@SpringConversion int []
new String [] { "123" , "456" } → new int [] { 123 , 456 }
Array → List
Source Type
Target Declaration
Example
int []
@SpringConversion List < Integer >
new int [] { 123 , 456 } → List . of ( 123 , 456 )
Integer []
@SpringConversion List < Integer >
new Integer [] { 123 , 456 } → List . of ( 123 , 456 )
String []
@SpringConversion List < Integer >
new String [] { "123" , "456" } → List . of ( 123 , 456 )
Array → Object
Source Type
Target Declaration
Example
int []
@SpringConversion int
new int [] { 123 , 456 } → 123
Integer []
@SpringConversion int
new Integer [] { 123 , 456 } → 123
String []
@SpringConversion int
new String [] { "123" , "456" } → 123
Array → String
Source Type
Target Declaration
Example
int []
@SpringConversion String
new int [] { 123 , 456 } → "123,456"
Integer []
@SpringConversion String
new Integer [] { 123 , 456 } → "123,456"
String []
@SpringConversion String
new String [] { "123" , "456" } → "123,456"
List → List
Source Type
Target Declaration
Example
List < String >
@SpringConversion List < Integer >
List . of ( "123" , "456" ) → List . of ( 123 , 456 )
Map → Map
Source Type
Target Declaration
Example
Map < String , String >
@SpringConversion Map < Integer , Double >
Map . of ( "1" , "123.4" , "2" , "234.5" ) → Map . of ( 1 , 123.4 , 2 , 234.5 )
String → Double
Source Type
Target Declaration
Example
String
@SpringConversion @NumberFormat ( style = PERCENT ) double
"42%" → 0.42
String → Duration
Source Type
Target Declaration
Example
String
@SpringConversion @DurationFormat ( style = SIMPLE ) Duration
"42ms" → Duration . ofMillis ( 42 )
String → List
Source Type
Target Declaration
Example
String
@SpringConversion List < Integer >
"123, 456" → List . of ( 123 , 456 )
String → LocalDate
Source Type
Target Declaration
Example
String
@SpringConversion LocalDate
"1970-01-01" → LocalDate . EPOCH
String → LocalDateTime
Source Type
Target Declaration
Example
String
@SpringConversion LocalDateTime
"1970-01-01T00:00:00" → LocalDateTime . of ( LocalDate . EPOCH , LocalTime . MIDNIGHT )