The documentation might be misleading. The minSize constraint will only apply to:
- String
- Arrays
- Classes which implements the java.util.Collection interface
java.util.Map however does not extend the java.util.Collection interface
See the supports
method of MinSizeConstraint
:
public boolean supports(Class type) {
return type != null && (
String.class.isAssignableFrom(type) ||
Collection.class.isAssignableFrom(type) ||
type.isArray());
}
You can develop your own custom constraint for this or a custom validator as suggested by Thermech
In addition, in order for Grails to mock the validate method properly your test class should be something like:
@TestMixin(ControllerUnitTestMixin) class FooTest {
@Test
void shouldNotValidateIfItemsIsEmpty() {
Foo foo = mockCommandObject Foo
foo.items = [:]
assert !foo.validate()
} }