djcrankypants / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

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

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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. 

Original issue reported on code.google.com by alexande...@googlemail.com on 10 Oct 2013 at 7:55

GoogleCodeExporter commented 8 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.

Original comment by pnied...@gmail.com on 10 Oct 2013 at 11:50

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

Original comment by jpibarra...@gmail.com on 17 Jul 2014 at 1:06

GoogleCodeExporter commented 8 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. ;-)

Original comment by alexande...@googlemail.com on 17 Jul 2014 at 5:01