0

In Spock, how can I test for withTransaction block? For example, during the test for the code below, how can I make sure that the code inside MyLookup.withTransaction gets executed?

then:
1 * MyLookup.findValue(item)

And here is service class:

class itemService {

    static String lookUpInfo(String item){  
        MyLookup.withTransaction {
            valInfo = MyLookup.findValue(item)
        }
        return valInfo
    }
}   
Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
CChang
  • 28
  • 1
  • 6
  • 2
    Would you mind explaining or - even better - showing the classes your are trying to test, such as `MyLookup`? This is called [MCVE](http://stackoverflow.com/help/mcve) and is incredibly helpful for people trying to help you because then they can actually reproduce your situation and maybe offer solutions or hints about how to refactor your code. – kriegaex Apr 09 '18 at 06:03
  • Are you using Grails? – Mene Apr 09 '18 at 07:59
  • Yes. Groovy Grails. – CChang Apr 11 '18 at 01:40

1 Answers1

0

So you can look at the PowerMock framework which enables you to test static methods see this post as to how.

Alternatively you can wrap the static method call in your own class and mock your wrapper class to make sure the call is made.

Hope this helps.

billy.mccarthy
  • 122
  • 1
  • 11