2
votes

Hi everyone I am working on a mock test, m y class extends of GroovyTestCase and I am trying to set up a property in the the void setUp method as you can see in:

 void setUp()
{
    def slurper = new JsonSlurper()
    inData = slurper.parse( new File( "src/test/resources/fixtures/v1/watch/20160511_watch_listings_live_in.json" ), 'UTF-8' )
    outData = slurper.parse( new File( "src/test/resources/fixtures/v1/watch/20160511_watch_listings_live_out.json" ), 'UTF-8' )

    watchPresenter = BinderTestUtils.instanceForLibraryNamed( "dang_v1_watch_presenter" )
    watchPresenter.localTranslate = new LocalTranslateHelperTest( )
    //def info = [ mapper: mapperMock]
    //watchPresenter:[localTranslate:new LocalTranslateHelperTest( )]
    println("watchPresenterTranslate:" + watchPresenter.localTranslate.getStrings("en"))
}

But I am getting the next error:

Cannot set readonly property: localTranslate for class WatchListingPresenterTests.

Do you know if is possible to set up a readOnly property as in this case?

In the real class I am using the localTranslate script just like this:

def strings = this.localTranslate.getStrings( params["lang"] )

I need to mock this property, but I am getting this error.

Thanks in advance.

1

1 Answers

5
votes

I solved this problem using the metaClass for get/setProperty. The documentation about runtime metaprogramming is in the next link:

http://groovy-lang.org/metaprogramming.html

My solution was very simple as you can see:

watchPresenter.metaClass .localTranslate = new LocalTranslateHelperTest( )

And that worked perfectly.

Thanks anyway.