Refactor Junit test code to remove duplication