どのように私は、複数の制約違反をチェックすることができますか?

コーダー:

私はつまり、私のモデルクラスで定義されている注釈検証するためのバリデータを作成している@NotNull@Pattern私は1件の違反を探している主張の下に作成され、私のテストケースを書くなくなりなど。しかし、いくつかのケースでは1以上の違反があります。どうすれば、複数の違反をチェックするには、以下の改正のですか?私は基本的に同様の主張をしたいです

result.getViolations().size(), is(1 or more)

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
    Validator validator = validatorFactory.getValidator();

    Set<ConstraintViolation<CreateRequest>> violations = validator.validate(createRequest);

    if (violations.isEmpty()) {
        CreateResponse createResponse = restTemplate.postForObject(clientProperties.getCreateUrl(), createRequest, CreateResponse.class);
        return new ClientResult<>(createResponse);
    } else {
        return new ClientResult<>(violations);
    }

        assertThat("Number of violations", result.getViolations().size(), is(1));
amseager:

あなたは、最後の行に変更することができます。

assertThat("Number of violations", result.getViolations().size(), is(oneOf(1, 2)));

参照ドキュメントのためのoneOf方法を。

Hamcrest 2.2でテスト済み

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=23542&siteId=1