如何在Grails Spock单元测试中初始化/绑定bean? [英] How to initialise/wire beans in Grails Spock unit tests?

查看:166
本文介绍了如何在Grails Spock单元测试中初始化/绑定bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要测试一个包含bean的Grails控制器(我将其工作时将其移至Service,但现在我想使其保持简单).

I'm wanting to test a Grails controller that contains a bean (I'll move it to a Service when I get it working, but I just want to keep it simple now).

//resources.groovy
beans {
    myBean(com.me.MyBean) 
}


// MyBean.java
// this needs to be in java as it is playing with spring-data-neo4j
package com.me;
public class MyBean {
    String show() {
        return "Hello";
    }
}

// TestController.groovy
package com.me

import com.me.MyBean

class TestController {

    def myBean

    def index() {
        render myBean.show()
    }
}


// TestControllerSpec.groovy
package com.me

import grails.test.mixin.TestFor
import spock.lang.Specification

import com.me.*

@TestFor(TestController)
class TestControllerSpec extends Specification {

    def myBean

    def setup() {
        defineBeans {
            myBean(com.me.MyBean) {bean->
                bean.autowire = true
            }
        }
    }

    def cleanup() {
    }

    def "show() returns Hello"() {
        when:
        def rc = controller.myBean.show()
        def rc2 = myBean.show()

        then:
        rc == "Hello"
        rc2 == "Hello"
    }
}

在TestControllerSpec中,myBean为null. controller.myBean也为null.我认为这是因为Spring不会拾取bean并将其连接.我收集到的是,在单元测试中,并非所有spring bean都可用,但是我需要做些什么才能使controller.myBean实例化并正确连接?

Within TestControllerSpec, myBean is null. controller.myBean is also null. I think this is because Spring is not picking the bean up and wiring it in. I gather that in unit tests not all spring beans are available, but what do I need to do to get controller.myBean to be instantiated and wired up correctly?

推荐答案

您必须按照以下方式模拟myBean

You must be mocking the myBean as below

def myBean = Mock(MyBean)

MyBean myBean = Mock()

,然后根据需要存根出方法,如下所示:

and then stub out method for your need if required as below:

myBean.show >> "test data"

,然后将其分配给已经为您模拟的控制器对象.

and then assign it to controller object which is already mocked for you.

controller.myBean = myBean

然后您就可以了.

或者可选地,您可以存根myBean并提供存根实现.例如,

Or optionally you can stub out myBean and give stubbed implementations. For example,

MyBean myBean = Stub(){
show() >> {return "sample text"}
}
controller.myBean = myBean

这样做的原因是我们不是在测试诸如控制器,视图或域之类的应用程序实体的集成,而是在测试一个单元即一个方法,因此我们应该仅对其进行测试,而对于集成,我们应该使用集成测试用例在所有情况下都是相似的,只是在正常情况下不需要任何模拟.

The reason for doing this is we are not testing the integration of application entities like controller, views or domain but we are testing a single unit i.e. a method and hence we should be just testing it and for integration we should be using integration test cases which would be similar in everything except you won't require any mocking in normal scenarios.

发现了另一个有用的功能,可以使用defineBeans闭包来模拟服务或bean,如下所示:

found another useful feature to mock services or beans using defineBeans closure as below:

        defineBeans {
            adapter(Adapter)
            helperService(HelperService)
        }

这将允许从grailsApplication访问bean.

This will allow beans to be accessed from grailsApplication.

希望有帮助.

这篇关于如何在Grails Spock单元测试中初始化/绑定bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆