1

I am trying to write a test case for this method, which works when it is hit from non test case code...

import grails.converters.XML
Class itemService(){

    def getData(Map){
        def helperBean = new HelperBean(Map)
        def requestXml = helperBean as XML
            -------- some code-------------
        return requestXml
        }
    }

To test this method, I have a test case like this:

 void testGetData(){

            def service = new itemService()
            def id="123"
            def map = [id:id.toLong()]
            def result = service.getData(map)
            assertNotNull(result)
            assertEquals "123", result.id
        }

But it keeps throwing this exception

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.xyz.groovy.utils.helperBean@443acc67' with class 'com.xyz.groovy.utils.HelperBean' to class 'grails.converters.XML'

at this line in the getData(Map method)

   def requestXml = userBean as XML

Can anyone help me out with why it is throwing that exception when the getData(Map) is being hit from the test case only? It is working fine when that method is hit from non test case code.

4

2 回答 2

0

您的测试类(testGetData() 方法所在的位置)实现了 ControllerUnitTestCase 类,或者您在哪里使用注释 @TestFor(SomeController)?

于 2014-02-21T16:19:23.017 回答
0

Found the solution. Adding addConverters(helperBean) in the setup() method of the test case should solve the problem.

于 2012-08-24T14:33:53.810 回答