Spock Final Class Layout

Can I release the final classes? If so, how? The search results raised this gist , which would seem to imply this, but I cannot find examples of this. I also found forum posts that say mocking final classes are not supported.

+6
source share
1 answer

This specification:

@Grab('org.spockframework:spock-core:1.0-groovy-2.4') @Grab('cglib:cglib-nodep:3.1') import spock.lang.* class Test extends Specification { def 'lol'() { given: def s = Mock(String) { size() >> 10 } expect: s.size() == 10 } } 

ends with the following exception:

 JUnit 4 Runner, Tests: 1, Failures: 1, Time: 29 Test Failure: lol(Test) org.spockframework.mock.CannotCreateMockException: Cannot create mock for class java.lang.String because Java mocks cannot mock final classes. If the code under test is written in Groovy, use Groovy mock. 

The solution is to use GroovyMock :

 @Grab('org.spockframework:spock-core:1.0-groovy-2.4') @Grab('cglib:cglib-nodep:3.1') import spock.lang.* class Test extends Specification { def 'lol'() { given: def s = GroovyMock(String) { size() >> 10 } expect: s.size() == 10 } } 

Which works well.

+14
source

All Articles