Generated from Example_Validations_Simple_Test.java at 3.0.0 2020-08-11T17:36:18Z

Predicate Validations

Abstract

Basic introduction (by example) to simple validations with library functions.

Examples

There is no need to actually explain much. Each of the predicate interfaces in the library have static methods 'throwIf' and 'throwIfNot' that basically can be used as a very simplistic validations or assertions (in production code).

Lets consider very simple validation that need to be done.


@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "'arg' cannot be greater or equal 50") public void test1() { //passes throwIf(arg45, arg -> arg >= 50, IllegalArgumentException::new, "'arg' cannot be greater or equal 50"); //passes throwIfNot(arg45, arg -> arg < 50, IllegalArgumentException::new, "'arg' cannot be greater or equal 50"); //fails throwIf(60, arg -> arg >= 50, IllegalArgumentException::new, "'arg' cannot be greater or equal 50"); }

Now lets include the value of the argument to the message.


@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "'arg'=45 cannot be greater or equal 40") public void test2() throws Exception { //fails throwIf(arg45, arg -> arg >= 40, IllegalArgumentException::new, "'arg'=%s cannot be greater or equal %s", arg45, 40); }

And here is a shorter version:


@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "'arg'=45 cannot be greater or equal 40") public void test3() { //fails throwIf(arg45, arg -> arg >= 40, IllegalArgumentException::new, "'arg'=%s cannot be greater or equal 40"); }

A more general version:


@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "'arg'=45 cannot be greater or equal 40") public void test4() { //fails LBiIntPredicate.throwIf(arg45, 40, (arg, threshold)-> arg >= threshold, IllegalArgumentException::new, "'arg'=%s cannot be greater or equal %s"); }

Bellow is more descriptive example, that references one of the methods from classes Be, Does, Is, P.


@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "'arg'=45 cannot be greater or equal 40") public void test5() { //fails throwIf(arg45, Is::gtEq, 40, IllegalArgumentException::new, "'arg'=%s cannot be greater or equal 40"); //would do exactly the same, but is less self explanatory throwIf(arg45, 40, Is::gtEq, IllegalArgumentException::new, "'arg'=%s cannot be greater or equal 40"); }

Library includes also class with factory methods for most common exceptions - X

Another good example:


@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "'arg'=45 must be between 5 and 40.") public void test6() { //fails throwIfNot(arg45, Is::between, 5, 40, X::arg, "'arg'=%s must be between 5 and 40."); //would do exactly the same, but you have full control over message. throwIfNot(arg45, Is::between, 5, 40, X::arg, "'arg'=%d must be between %d and %d."); }

AssertionError example:


@Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = "'arg'=45 must be between 5 and 40.") public void test7() { throwIfNot(arg45, Is::between, 5, 40, X::assertion, "'arg'=%s must be between 5 and 40."); }

Generated from Example_Validations_Simple_Test.java at 3.0.0 2020-08-11T17:36:18Z