Factor out boiler plate test code

use interface and closure to factor out some repetitive code.