简体   繁体   English

模拟服务和Grails jaxrs集成测试

[英]Mock-service and Grails jaxrs integration test

I want to test a grails application(using jaxrs) and it's integrations. 我想测试grails应用程序(使用jaxrs)及其集成。 For this task I hope to use the awesome IntegrationTestCase-class from the jaxrs plugin. 对于此任务,我希望使用来自jaxrs插件的出色的IntegrationTestCase类。 This is challenging because I want to mock/replace a service within my application. 这具有挑战性,因为我想在我的应用程序中模拟/替换服务。 With “pure” Spring I would create testcontext and manually wire up the mock. 使用“纯” Spring,我将创建testcontext并手动连接模拟。 I have no idea how to do this in grails. 我不知道该怎么做。 I've tried to access the ApplicationContext directly 我试图直接访问ApplicationContext

Holders.grailsApplication.mainContext.registerMockBean("myService", new MyMock())

This does not seem to work as the mock is not used during the test. 这似乎不起作用,因为在测试过程中未使用该模拟。 Any ideas? 有任何想法吗?

Try to define in test class his way: 尝试在测试类中以他的方式定义:

    @Before
    void before() {
        Holders.grailsApplication = grailsApplication

        defineBeans {
            myService(MyMock)
        }
    }

It's for jUnit tests. 它用于jUnit测试。 If you use Spock, rename before() to setup() and see Spock basics Fixture Methods. 如果使用Spock,则将before()重命名为setup(),然后参阅Spock基础知识夹具方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM