Limitations
-
You can not mock an interface, you must mock the actual implementation. [1]
-
You should probably only mock application singletons
Requirements
Some additional changes need to be made to your Maven pom.xml
Groovy Compiler
You need to add the groovy compiler
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6.2</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.5.6</version>
<scope>runtime</scope>
<type>pom</type>
</dependency>
</dependencies>
</plugin>
ByteBuddy
ByteBuddy is needed for mocking
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.9.12</version>
</dependency>
Sample
Here is a sample of a Spock Specification
@QuarkusSpec (1)
class MySpec extends Specification {
@Inject
MySimpleBeanService service
@Inject
SimpleBean1 simple1
def "Make Sure My Stuff Mocks"(){
setup:
simple1.get() >> "OK" (3)
expect:
service.callSimple1sMethod() == "OK"
}
@Mocks (2)
SimpleBean1Impl mock(){
return Mock(SimpleBean1Impl)
}
@Mocks (2)
SimpleBean2Impl mock(){
return Stub(SimpleBean2Impl)
}
-
Annotate your Specification with
@QuarkusSpec
-
Create a method that returns the actual implementation annotate with
@Mocks
-
Mock like normal in your
setup:
block