我对测试有点陌生。我有一个带有spring boot和oracle db的小应用程序。当我的JavaBean类可以具有空的ID int值时,我想编写一个测试。这甚至可以测试吗?

目前,我有此代码,但我认为它不正确。

ReportModel reportModel = new ReportModel();

Validator validator = createValidator();
Set<ConstraintViolation<ReportModel>> constraintViolationSet = validator.validate(reportModel);

assertThat(constraintViolationSet.size()).isEqualTo(0);


编辑:完整测试类:

public class ValidatorTests {

    private Validator createValidator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.afterPropertiesSet();
        return localValidatorFactoryBean;
    }

    @Test
    public void shouldNotValidateWhenIdIsNull() {
        LocaleContextHolder.setLocale(Locale.ENGLISH);
        ReportModel reportModel = new ReportModel();

        Validator validator = createValidator();
        Set<ConstraintViolation<ReportModel>> constraintViolationSet = validator.validate(reportModel);

        assertThat(constraintViolationSet.size()).isEqualTo(0);
    }
}

最佳答案

尝试这个:

assertEquals(constraintViolationSet.size(), 0);


要么,

assertTrue(constraintViolationSet.isEmpty());

10-05 18:19