spockframework / spock

The Enterprise-ready testing and specification framework.
https://spockframework.org
Apache License 2.0
3.54k stars 466 forks source link

Support mocking final Java classes and stubbing static Java methods #455

Closed robfletcher closed 9 years ago

robfletcher commented 9 years ago

Originally reported on Google Code with ID 333

I just love Spock, but our code base is 100% Java. Tons of code which cannot be refactored
all at once uses stuff like utility classes with only static methods. So as to avoid
the classes from being instantiated or subclassed they are final and have private constructors
which are never called, similar to java.lang.Math. As you can imagine, these classes
are hard to mock-test without frameworks like PowerMock. But PowerMock is incompatible
with Spock/Groovy AFAIK.

I would love to see features similar to PowerMock in Spock. I know I can mock/stub
Groovy code of this kind with Spock, but not Java code. I request an extension or enhancement
to enable me to use Spock for these cases. Basically I do not care if you somehow make
Spock compatible with PowerMock or if you incorporate the feature directly into Spock.
I would prefer the latter, though. 

Reported by alexander.kriegisch on 2013-10-10 07:55:27

robfletcher commented 9 years ago
It's not something we intend to implement anytime soon. If PowerMock is still incompatible
with Groovy/Spock, maybe you can work with the PowerMock folks to fix these problems
(at least the Groovy ones). Alternatively, try JMockit.

Reported by pniederw on 2013-10-10 11:50:35

robfletcher commented 9 years ago
FYI, PowerMock works with Spock. See - http://stackoverflow.com/questions/19493690/using-powermock-with-spock/24749047#24749047

Reported by jpibarra1130 on 2014-07-17 13:06:45

robfletcher commented 9 years ago
Thank, and FYI, I am the guy (kriegaex) who published the solution on GitHub. Anyway,
I would prefer not having to use PowerMock in Spock. One-stop shopping is nicer. ;-)

Reported by alexander.kriegisch on 2014-07-17 17:01:08