Skip to content

xiaozhukk/spock-inject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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()
    }
}

About

auto inject dependenceies for spock framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages