0
0
JUnittesting~5 mins

Argument conversion in JUnit

Choose your learning style9 modes available
Introduction

Argument conversion helps JUnit tests accept different types of inputs easily. It changes input data into the right form for the test.

When you want to run the same test with different types of input data.
When your test method needs complex objects instead of simple strings or numbers.
When you want to keep your test code clean by separating data conversion logic.
When using parameterized tests that require converting input arguments automatically.
Syntax
JUnit
@ParameterizedTest
@ValueSource(strings = {"input1", "input2"})
void testMethod(@ConvertWith(MyConverter.class) MyType arg) {
    // test code
}

// Converter class example
class MyConverter extends SimpleArgumentConverter {
    @Override
    protected Object convert(Object source, Class<?> targetType) {
        // conversion logic
        return null; // placeholder return
    }
}

The @ConvertWith annotation tells JUnit to use a custom converter for the argument.

The converter class must extend SimpleArgumentConverter and override the convert method.

Examples
This example converts string inputs to integers before testing if they are positive.
JUnit
@ParameterizedTest
@ValueSource(strings = {"123", "456"})
void testWithInteger(@ConvertWith(IntegerConverter.class) Integer number) {
    assertTrue(number > 0);
}

class IntegerConverter extends SimpleArgumentConverter {
    @Override
    protected Object convert(Object source, Class<?> targetType) {
        return Integer.parseInt(source.toString());
    }
}
This example converts a comma-separated string into a Person object for testing.
JUnit
@ParameterizedTest
@ValueSource(strings = {"John,25", "Jane,30"})
void testWithPerson(@ConvertWith(PersonConverter.class) Person person) {
    assertNotNull(person.getName());
}

class Person {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() { return name; }
}

class PersonConverter extends SimpleArgumentConverter {
    @Override
    protected Object convert(Object source, Class<?> targetType) {
        String[] parts = source.toString().split(",");
        return new Person(parts[0], Integer.parseInt(parts[1]));
    }
}
Sample Program

This test converts string inputs "true" or "false" into Boolean objects using a custom converter. It checks that the conversion is not null and prints the converted value.

JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.SimpleArgumentConverter;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.ValueSource;

class ArgumentConversionTest {

    static class BooleanConverter extends SimpleArgumentConverter {
        @Override
        protected Object convert(Object source, Class<?> targetType) {
            return Boolean.parseBoolean(source.toString());
        }
    }

    @ParameterizedTest
    @ValueSource(strings = {"true", "false", "true"})
    void testBooleanConversion(@ConvertWith(BooleanConverter.class) Boolean flag) {
        assertNotNull(flag);
        System.out.println("Converted value: " + flag);
    }
}
OutputSuccess
Important Notes

Always ensure your converter handles invalid input gracefully to avoid test failures.

Use argument conversion to keep your test methods simple and focused on testing logic.

Summary

Argument conversion changes input data into the needed type for tests.

Use @ConvertWith with a converter class extending SimpleArgumentConverter.

This helps run parameterized tests with complex or custom data types.