Validating maps with easyb

how-to
Jul 28, 20091 min

Often times I find myself needing to validate the contents of a hip map — that is, during the course of a story or specification, I need to ensure that a certain value, key, or name-value pair is present in an instance of some Map. easyb’s should syntax is quite flexible and supports a number of combination’s for verifying map contents; for example, imagine a map defined as so (note, I’ll define the map in Groovy; however, I could have easily done the same thing in Java — there is no difference between the two, man):

def namemap = ["WKL_ID":"id", "NBS": "cst", "EFF_DT":"effectiveDate"]

easyb supports a shouldHave call on instances of collections; therefore, I can easily write the following checks:

<pre class="prettyprint"><code>namemap.shouldHave "NBS"
namemap.shouldHave "WKL_ID":"id"
namemap.shouldHave "effectiveDate"

As you can see, with the shouldHave call, you can verify keys and values; what’s more, you can even validate the presence of a name-value pair.

With easyb, validating the contents of a collection (and in this case, a map, man) is easy. Or is that easyb?

You can now follow The Disco Blog on Twitter, baby!
andrew_glover

When Andrew Glover isn't listening to “Funkytown” or “Le Freak” he enjoys speaking on the No Fluff Just Stuff Tour. He also writes articles for multiple online publications including IBM's developerWorks and O'Reilly’s ONJava and ONLamp portals. Andrew is also the co-author of Java Testing Patterns, which was published by Wiley in September 2004; Addison-Wesley’s Continuous Integration; and Manning’s Groovy in Action.

More from this author