@Base64¶
@Base64 is an annotation that decodes Base64 encoded instances of type
byte[] or String into byte[] instances:
import io.github.scordio.junit.converters.Base64;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.FieldSource;
@ParameterizedTest
@FieldSource("encoded")
void test(@Base64 byte[] bytes) {
assertArrayEquals(new byte[] { 63, 63, 63 }, bytes);
}
static List<?> encoded = List.of("Pz8/", new byte[] { 80, 122, 56, 47 });
The annotation's optional encoding attribute configures the desired encoding scheme:
BASIC(default): for the Basic encoding schemeURL: for the URL and Filename Safe encoding schemeMIME: for the MIME encoding scheme
The following source types and target declarations are supported.
| Source Type | Target Declaration | Example |
|---|---|---|
byte[] |
@Base64 byte[] |
new byte[] { 80, 122, 56, 47 } → new byte[] { 63, 63, 63 } |
byte[] |
@Base64(encoding = URL) byte[] |
new byte[] { 80, 122, 56, 95 } → new byte[] { 63, 63, 63 } |
byte[] |
@Base64(encoding = MIME) byte[] |
new byte[] { 80, 122, 13, 10, 56, 47 } → new byte[] { 63, 63, 63 } |
String |
@Base64 byte[] |
"Pz8/" → new byte[] { 63, 63, 63 } |
String |
@Base64(encoding = URL) byte[] |
"Pz8_" → new byte[] { 63, 63, 63 } |
String |
@Base64(encoding = MIME) byte[] |
"Pz\r\n8/" → new byte[] { 63, 63, 63 } |