Witam. Mam prostą klasę Car, do której został napisany walidator sprawdzający poprawność jego pól. Chciałem napisać test tego walidatora aby sprawdzić czy działa on poprawnie. Klasę testową napisałem jako test parametryzowany. Jednak wydaje mi się, że nie jest ona czytelna i można się łatwo pogubić, jeśli mamy wiele przypadków testowych jak np. w walidatorze. Mamy tu bardzo dużą tablicę z wieloma elementami, które przyjmują z kolei dużo parametrów. Poniżej klasa:
@RunWith(value = Parameterized.class)
public class CarValidatorTest {
private CarValidator carValidator;
private Car car;
private boolean result;
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{1L, "Ferrari", "120C", BigDecimal.valueOf(1000000), "Best car in the world", 1L, true},
{1L, "Ferrari", StringUtils.repeat("#", 3), BigDecimal.valueOf(1000000), StringUtils.repeat("#",
10), 1L, true},
{1L, "Ferrari", StringUtils.repeat("#", 45), BigDecimal.valueOf(1000000), StringUtils.repeat("#",
250), 1L, true},
{null, "Ferrari", "120C", BigDecimal.valueOf(1000000), "Best car in the world", 1L, true},
{1L, "F", "120C", BigDecimal.valueOf(1000000), "Best car in the world", 1L, false},
{1L, null, "120C", BigDecimal.valueOf(1000000), "Best car in the world", 1L, false},
{1L, "Ferrari", "1", BigDecimal.valueOf(1000000), "Best car in the world", 1L, false},
{1L, "Ferrari", StringUtils.repeat("#", 46), BigDecimal.valueOf(1000000),
"Best car in the world", 1L, false},
{1L, "Ferrari", null, BigDecimal.valueOf(1000000), "Best car in the world", 1L, false},
{1L, "Ferrari", "120C", BigDecimal.valueOf(-1), "Best car in the world", 1L, false},
{1L, "Ferrari", "120C", null, "Best car in the world", 1L, false},
{1L, "Ferrari", "120C", BigDecimal.valueOf(1000000), "Best", 1L, false},
{1L, "Ferrari", "120C", BigDecimal.valueOf(1000000), StringUtils.repeat("#", 251), 1L, false},
{1L, "Ferrari", "120C", BigDecimal.valueOf(1000000), null, 1L, false},
{1L, "Ferrari", "120C", BigDecimal.valueOf(1000000), "Best car in the world", null, false}
});
}
public CarValidatorTest(Long id, String carMake, String carModel, BigDecimal price,
String description, Long carGroupId, boolean result) {
this.car = new Car(id, carMake, carModel, price, description, carGroupId);
this.result = result;
}
@Before
public void setUp() {
carValidator = new CarValidator();
}
@Test
public void testIsValidCityName() throws SQLException {
assertEquals(result, carValidator.validateAdd(car).isRight());
}
}
Czy macie jakąś lepszą praktykę testowania tego typu klas?