JUnit:在创建JavaBean时测试int类型的ID是否为null

时间:2016-09-09 09:16:53

标签: java junit spring-boot

我对测试有点新意。我有一个小应用程序与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);
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

assertEquals(constraintViolationSet.size(), 0);

或,

assertTrue(constraintViolationSet.isEmpty());