spock-inject
auto inject dependencies for spock framework
help Mock and inject beans for Spock unit test framework.
some framework(eg: Spring) help manage beans relations on the runtime, and probability have no setter method for dependent bean. it is hard to Mock the dependent beans.
this extension will make it easy to achieve this purpose by only several Annotations.
@Subject
indicate the Object to be tested. this annotation is primitive provided by Spock framework, and we just reuse it. @Mock
indicate Objects/beans are required by the Object which to be tested
Repository
<dependency>
<groupId>com.github.xiaozhukk</groupId>
<artifactId>spock-inject</artifactId>
<version>1.0.0</version>
</dependency>
How to use
run SpockAnnotation.init(this) in the setup method
eg:
class Foo {
@Resource
private Bar bar;
public void methodToBeTested() {
bar.method1();
// ...
}
}
class Bar {
public boolean method1() {
// ...
}
}
class FooTest extends Specification{
@Subject
Foo foo
@Mock
Bar bar
void setup() {
SpockAnnotation.init(this)
}
def "foo test"() {
given:
bar.method1() >> true
when:
foo.mehodToBeTested()
then:
1 * bar.method1()
}
}