Spock Helper Methods, How To Ignore Methods As Tests

Spock Unit Test Utility Methods

I’ve been writing more and more Spock unit tests and I wanted to create a ‘helper’ method, i.e. a method to make the tests cleaner and to prevent duplication.

The problem is that if you create a method using def e.g. def myMethod() {} IntelliJ puts the green “run” button next to the method making it look like a test, thankfully however Spock doesn’t treat this as a test.

Private Method

You can create these methods as private, and they may as well be static too.

Now private static myMethod() {} will not be indicated as a test but def myMethod() {} looks nicer. I will stick with def and wait for IntelliJ to fix this bug.