In some use-cases (e.g.: bitwise operations) you might want to work with numbers in a byte array form. The conversion from number to byte array in this case would ideally not be part of your test. By applying @NumberToByteArrayConversion on the parameter you want to convert, you can immediately start working with a byte[] in your test.

@NumberToByteArrayConversion

When applied to a parameter supplied with a number by @ValueSource, @NumberToByteArrayConversion converts the number to its byte[] representation. To showcase with some examples:

@ParameterizedTest
@ValueSource(ints = { 1025 })
void intExample(@NumberToByteArrayConversion byte[] bytes) {
    assertThat(bytes).hasSize(4).containsExactly(0, 0, 4, 1);
}

@ParameterizedTest
@ValueSource(longs = { 393796333641L })
void longExample(@NumberToByteArrayConversion byte[] bytes) {
    assertThat(bytes).hasSize(8).containsExactly(0, 0, 0, 91, 176, 23, 48, 73);
}

By default, the argument converter uses big endian order. This can be configured in the annotation using order.

@ParameterizedTest
@ValueSource(ints = { 1025 })
void test(@NumberToByteArrayConversion(order = NumberToByteArrayConversion.ByteOrder.LITTLE_ENDIAN) byte[] bytes) {
    assertThat(bytes).hasSize(4).containsExactly(1, 4, 0, 0);
}

The argument converter will support byte, short, int, long, double and float. It does not support String.

Thread-Safety

This argument converter is safe to use during parallel test execution.