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

  • SEARCH
  • Home
  • 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 7910861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:01:55+00:00 2026-06-03T13:01:55+00:00

I got following test case below and have a question to it. Why does

  • 0

I got following test case below and have a question to it. Why does the validation of a nested property without the groups parameter always fail? How can I achieve a validation of the nested properties without groups?

Thx in advance

Kind regards

Christian

My Test case:

    package validation;
    import java.util.Set;
    import javax.validation.*;
    import javax.validation.constraints.*;
    import junit.framework.Assert;
    import lombok.Data;
    import lombok.extern.slf4j.Slf4j;
    import org.junit.*;

    @Slf4j
    public class NestedPropertiesTest {

        private ValidatorFactory factory;
        private Validator validator;

        @Before
        public void setUp() throws Exception {
            log.info("\n\nTest start:");
            this.factory = Validation.buildDefaultValidatorFactory();
            this.validator = factory.getValidator();
        }

        @Test
        public void testNestedBarNullValue() {
            Foo foo = new Foo();
            Bar bar = new Bar();
            bar.setNotempty(null);
            foo.setBar(bar);
            log.info("With groups:");
            Set<ConstraintViolation<Foo>> errors = validator.validateProperty(foo, "bar.notempty", Checks.class);
            logErrors(errors);
            Assert.assertFalse(errors.isEmpty());
            log.info("Without groups:");
            errors = validator.validateProperty(foo, "bar.notempty");
            logErrors(errors);
            Assert.assertFalse(errors.isEmpty());
        }

        @Test
        public void testNestedBarMinSize() {
            Foo foo = new Foo();
            Bar bar = new Bar();
            bar.setNotempty("1");
            foo.setBar(bar);
            log.info("With groups:");
            Set<ConstraintViolation<Foo>> errors = validator.validateProperty(foo, "bar.notempty", Checks.class);
            logErrors(errors);
            Assert.assertFalse(errors.isEmpty());
            log.info("Without groups:");
            errors = validator.validateProperty(foo, "bar.notempty");
            logErrors(errors);
            Assert.assertFalse(errors.isEmpty());
        }

        @SuppressWarnings("rawtypes")
        private void logErrors(Set errors) {
            log.info("Nr. of validation errors: {}", errors.size());
            for (Object error : errors) {
                ConstraintViolation cv = (ConstraintViolation) error;  
                log.info("{}: {}", cv.getPropertyPath(), cv.getMessage());
            }
        }

        @Data private class Foo {
            @Valid private Bar bar;
        }
        @Data private class Bar {
            @NotNull(groups=Checks.class)
            @Size(min = 2, groups=Checks.class)
            private String notempty;
        }
        private interface Checks {}
    }

My pom (just in case):

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>noorg</groupId>
        <artifactId>validation</artifactId>
        <version>0.0.1-SNAPSHOT</version>

        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>4.2.0.Final</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>0.11.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.6.4</version>
            </dependency>
        </dependencies>
    </project>
  • 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-06-03T13:01:57+00:00Added an answer on June 3, 2026 at 1:01 pm

    Once you specify a group, your constraint is no longer implicitly part of the default group. If you want your annotation to also still be in the default group, then you need to specify it.

        @Data private class Bar {
            @NotNull(groups={Checks.class, javax.validation.groups.Default.class})
            @Size(min = 2, groups={Checks.class, javax.validation.groups.Default.class})
            private String notempty;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following problem (test version available http://jsfiddle.net/qmP8R/2/ ): I have two <div>
The below test case runs out of memory on 32 bit machines (throwing std::bad_alloc)
I've got following php code: $match = array(); if (preg_match(%^(/\d+)(/test)(/\w+)*$%, /25/test/t1/t2/t3/t4, $match)) print_r($match); I'm
I got following code in an XHTML-document and every browser saysit is malformed :(
I got following error... System.NullReferenceException: Object reference not set to an instance of an
I got following code from net and it looks everything is proper but i'm
I've got following problem: (c#) There is some class (IRC bot), which has method,
i've got following example xml: <entity id=1> <name>computer</name> <type>category</type> <entities> <entity id=2> <name>mac</name> <type>category</type>
I've got following URL in symfony (specifics not important): /frontend_dev.php/something/25/apple ... and a routing
I've got following (simplified for example purpose) code and it works: void log(const string

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.