简体   繁体   English

Grails-如何-使用控制器中的参数的单元测试服务

[英]Grails - How to - Unit test Service that uses params from controller

I'm trying to unit test a service method that takes as parameters a date and the params LinkedHashMap from the controller. 我正在尝试对服务方法进行单元测试,该服务方法将日期和控制器中的参数 LinkedHashMap作为参数。 The setup is as follows: 设置如下:

def save = {CustomCommandObject erc ->
...
    if (erc.conditionProperty != null) {
        def result = myService.ServiceMethod(someDate, params)
    ...
    }
    ....
    redirect(controller: 'controllerName', action: 'actionName', id: params.id)
}

class MyService {
    static transactional = false
    ....
    def TypeToReturn ServiceMethod(Date someDate, def params){
    ...
        TypeToReturn typeToReturn = new TypeToReturn(params)
        return typeToReturn
    }
    ....
}

One of the tags on the GSP view is of type < joda:timerPicker > : GSP视图上的标记之一是<joda:timerPicker>类型:

and TypeToReturn has a property: 并且TypeToReturn具有一个属性:

LocalTime propertyName

When the application runs normally in the browser the params map is passed correctly to the service method and new TypeToReturn(params) creates an instance of TypeToReturn . 当应用程序在浏览器中正常运行时,参数映射将正确传递给服务方法, 新的TypeToReturn(params)创建一个TypeToReturn实例。 params contains the following: 参数包含以下内容:

{java.util.LinkedHashMap$Entry@xxxxx} propertyName_hour -> 20
{java.util.LinkedHashMap$Entry@xxxxx} propertyName_minute -> 30
{java.util.LinkedHashMap$Entry@xxxxx} propertyName -> struct

Therefore, in my test for the service method i have the following: 因此,在对服务方法的测试中,我具有以下几点:

void testSomething() {
    LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
    params.put("propertyName","struct")
    params.put("propertyName_hour", "20")
    params.put("propertyName_minutes", "30")
    myService = new MyService()
    Date d = new Date()
    myService.ServiceMethod(d,params)
}

However when i try and run the test i get the following error: 但是,当我尝试运行测试时,出现以下错误:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: 
Cannot cast object 'struct' with class 'java.lang.String' to class
'org.joda.time.LocalTime'

If i ommit params.put("propertyName","struct") from the map completely then again it (correctly) errors saying that TypeToReturn does not have a property propertyName_hour 如果我完全从地图上省略了params.put(“ propertyName”,“ struct”) ,则再次(正确)错误提示TypeToReturn没有属性propertyName_hour

How therefore, should I be unit testing such a service method that uses the params Map from the controller? 因此,我应该如何对使用控制器中的params Map的服务方法进行单元测试? Simply recreateing the map in the test doesn't seem to work. 仅在测试中重新创建地图似乎不起作用。 Should the params Map be mocked/stubbed in a partiular way? 应该以特殊方式对参数Map进行模拟/插入吗?

Thanks 谢谢

Are you running your test as a unit or integration test? 您是将测试作为单元测试还是作为集成测试运行? If it's not already there, try moving it into test/integration and run it from there using 如果尚不存在,请尝试将其移至测试/集成并使用

grails test-app integration:

That runs the test within the Grails environment and will give the joda-time plugin the opportunity to do whatever meta programming magic it's doing to create the type conversion to DateTime it looks like you're missing. 它将在Grails环境中运行测试,并将为joda-time插件提供机会,使其可以执行任何元编程魔术,以创建看上去像您所缺少的类型转换为DateTime的功能。

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

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