Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails Unit Test Service MissingProperty 'log'

I want to run a unit test for a service. The method I want to test includes a some log.debug() statements. While the log property is injected at runtime, it does not seem to be injected in tests, so it throws groovy.lang.MissingPropertyException: No such property: log for class:

This is my unit test class:

@TestFor(ServiceUnderTest)
@Mock([ServiceUnderTest])
class ServiceUnderTestTests {

    def test() {
        def mock = [ mockedProp: [...] ] as ServiceUnderTest
        def info = mock.doOperation()
        assert ....
    }
}

I've also tried adding MockUtils.mockLogging(ServiceUnderTest) but with no success.

How can I get the log property properly injected in my service class while in unit tests?

like image 921
matcauthon Avatar asked Nov 30 '25 20:11

matcauthon


1 Answers

You do not have to have the test class annotated with @Mock([ServiceUnderTest]). @TestFor(ServiceUnderTest) detects its a service class and does all the mocking automatically. It also adds a service property to the test class that can be accessed in all the test methods and mocks the log property accordingly.

I think the problem why neither mocking nor explicit log mocking with MockUtils.mockLogging(ServiceUnderTest) does work in your case is the as coercion you are using in your test method code:

def mock = [ mockedProp: [...] ] as ServiceUnderTest

Groovy internally uses java.lang.reflect.Proxy to create a proxy descendant class from ServiceUnderTest. The proxy class does not see changes done to the ServiceUnderTest meta class like the added log property.

I would solve this issue by using a per-object meta class. You can mock the property getter (or setter) by altering the metaClass of the service object. Be aware that meta-class changes are rolled back by Grails in-between execution of test methods:

service.metaClass.mockedProp = {-> ... }
like image 127
Andre Steingress Avatar answered Dec 02 '25 18:12

Andre Steingress



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!