mercredi 3 février 2016

Stub Method in Spring Spock (Groovy) not being executed

I have the following unit test I am working on.

@ContextConfiguration(
        loader = SpringApplicationContextLoader,
        classes = ManagementApplication.class
)

    @WebAppConfiguration

class CTest extends Specification {


    @Autowired
    ClRepository clRepository

    @Autowired
    CoService coService

    @Autowired
    ClService clService

    @Autowired
    UserRepository userRepository

    @Autowired
    TopicRepository topicRepository

    @Autowired

BasicAuthService authService

TopicService topicService  =  Spy(TopicService){
    createTopicForCo(_) >> null
}

def setup(){

}

def "Simple Test Create"() {
    given:
    def cl = ClRepository.findOne(1L)
    def sess = new Co(active: true, title: "Testing", cl: cl)
    topicService.createTopicForCo(sess) >> null
    ReflectionTestUtils.setField(coService, "topicService",topicService);

    when:
    println "Testing creation of coaching session"
    sess = coService.create(sess)

    then:
    def coachingSessions = coService.findAll()
    println coachingSessions
    coachingSessions.size() > 0
}
}

I see that a topicService Mock object is being created. ReflectionTestUtils.setField changes the topicService within coService with the Mocked one.

However, when createTopicForCo is called from within the topicService reference contained coService, the original implementation is called instead of my createTopicForCo(_) >> null.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire