Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5954345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:57:28+00:00 2026-05-22T17:57:28+00:00

The Hibernate Validator documentation has a simple Getting Started guide which outlines testing validation

  • 0

The Hibernate Validator documentation has a simple Getting Started guide which outlines testing validation rules.

The relevant chunk is

@Test
public void manufacturerIsNull() {
    Car car = new Car(null, "DD-AB-123", 4);

    Set<ConstraintViolation<Car>> constraintViolations =
        validator.validate(car);

    assertEquals(1, constraintViolations.size());
    assertEquals("may not be null", constraintViolations.iterator().next().getMessage());
}

It seems to me a fairly vague route to test if the NotNull constraint has been violated.

My simple solution would be something like

public static <T> boolean  containsConstraintViolation(
    Set<ConstraintViolation<T>> violations, Class<?> constraint) {

    for (ConstraintViolation<?> violation : violations) {
        ConstraintDescriptor<?> descriptor = violation.getConstraintDescriptor();
            if (constraint.isAssignableFrom(descriptor.getAnnotation().getClass()))
                return true;
        }
    return false;
}

Which allows me to do tests like

assertTrue(ValidationUtils.containsConstraintViolation(violations, NotNull.class));

However, I’m sure this is going to be naive in the long run and am wondering if there’s not some other library or API I’m missing to assist in unit testing constraints.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:57:29+00:00Added an answer on May 22, 2026 at 5:57 pm

    You could have a look at the class org.hibernate.validator.test.util.TestUtil which is used for Hibernate Validator’s own tests and offers functionality for testing expected constraint violations (amongst others there is assertCorrectConstraintTypes() for example).

    Just note, that this class is not part of Hibernate Validator’s public API, so it may be best to just use it to get some ideas.

    When comparing error messages always be sure to have the VM’s locale correctly set. Even better is to match against localized messages loaded via the correct resource bundle (org.hibernate.validator.ValidationMessages for the standard constraints in the case of Hibernate Validator).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using hibernate validator framework with Spring. A class implementing the Spring Validator validates
Hibernate has a handful of methods that, one way or another, takes your object
According to the NHibernate Validator documentation here: http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation.aspx I should be able to pass
The Hibernate validator issues tracker appears to indicate that a particular issue I am
I started creating JPA/hibernate mappings for a legacy database based on Oracle. At one
There must be a simple solution out there, I'm trying to use NHibernate Validator
Hibernate's validator is my JSR-303 provider in my Spring MVC application. When I use
I'm learning Hibernate Validator. I see that I can add an error message text
Is it possible to use the Hibernate validator API storing the validated object in
I'm trying to set up a custom message source for Hibernate Validator 4.1 through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.