Issue
I'm writing a JUnit test to assert that my algorithm's output object does not present any null value or empty strings.
For simplicity imagine 3 classes : Parent, Child, Car, where Parent is the object that I have to validate.
@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Builder
public class Child {
String name;
int age;
}
@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Builder
public class Car {
String brand;
String model;
}
@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Builder
public class Parent {
String name;
int age;
List<Child> children;
Car car;
}
what is the best and easy way to search for null values or empty strings?
I'm currently using the following method as a validator, checking field by field for null values and empty strings.
private boolean isValid(Parent parent) {
if(parent == null) return false;
boolean isObjectNull = Stream.of(parent.getName(), parent.getChildren(), parent.getCar()).anyMatch(Objects::isNull);
if(isObjectNull) return false;
isObjectNull = Stream.of(parent.getCar().getBrand(), parent.getCar().getModel()).anyMatch(Objects::isNull);
if(isObjectNull) return false;
for(Child child : parent.getChildren()){
isObjectNull = Stream.of(child.getName()).anyMatch(Objects::isNull);
if(isObjectNull) return false;
if(!isValidString(child.getName())) return false;
}
return isValidString(parent.getName(), parent.getCar().getBrand(), parent.getCar().getModel());
}
private boolean isValidString(String... values){
for(String s : values){
if(s.isEmpty())
}
}
But I would love something I can also use for other objects I will create in the future.
Solution
You can use reflection to obtain all the getters from your objects that do return a reference (instead of a primitive). Then iterate over that list (or array, your choice) and execute them; when the return value for any of these is null
, return false
or throw an appropriate exception.
A little bit like this:
public final void validateNonNull( final Object candidate ) throws ValidationException
{
if( isNull( candidate ) throw new ValidationException( "candidate is null" );
final var candidateClass = candidate.getClass();
final List<Method> getters = Arrays.stream( candidateClass.getMethods() ) // getters are always public!
.filter( m -> !m.getName().equals( "getClass" ) )
.filter( m -> m.getName().startsWith( "get" ) )
.filter( m -> m.getParameterCount() == 0 )
.filter( m -> !m.getReturnType().isPrimitive() )
.collect( Collectors.toList() );
for( var method : methods )
{
if( isNull( method.invoke( candidate ) ) throw new ValidationException( "candidate.%s() returned null".formatted( method.getName() ) );
}
}
ValidationException
is a custom exception, and you need to declare the checked exceptions that are declared for Method::invoke
.
To check for empty Strings, too, change the for
loop like this:
…
for( var method : methods )
{
var retValue = method.invoke( candidate );
if( retValue instanceof String aString && aString.isEmpty() ) throw new ValidationException( "candidate.%s() returned the empty String".formatted( method.getName() ) );
if( isNull( retValue ) throw new ValidationException( "candidate.%s() returned null".formatted( method.getName() ) );
}
Answered By - tquadrat
Answer Checked By - Terry (JavaFixing Volunteer)