I got this error after adding an onKernelRequest listener to my Symfony project and trying to pass Twig_Environment to it as an argument. It worked via the web browser, but not in any PHPUnit tests hitting multiple pages.

After much head scratching and reading of the Symfony source code, I ended up making an onKernelException listener instead. Magically, rendering a twig template from it (passing a TwigEngine argument) worked.

So, here's how I handle custom exceptions in a custom way. Notice I'm testing for Twig_Error_Runtime because my specific error is coming via Twig; you may be able to just test for regular Symfony exceptions instead:

https://gist.github.com/zyphlar/e1089be4c0f0499ab08b