During a Maven build with forked JUnit tests, this is especially problematic, as the focus keeps switching. Normally you solve this, by specifying the command line switch
-Djava.awt.headless=true
However when forking JUnit tests, this doesn't work. The trick is that not all switches are passed into the newly forked processes. However if you put the following in your build.properties, these will be passed, and you're one step closer to living a pain-free life with your Mac...
file: build.properties
java.awt.headless=true
maven.junit.sysproperties=java.awt.headless