Refactor middleware tests using application stubs