views:

1001

answers:

4

In the grails-framework some objects are using log. This is normally injected by grails. It works on execution of 'grails test-app'. But the same test (an integration-test) fails on execution of 'grails test-app -integration'.

What goes wrong here and can I force the injection of the log-object somehow?

+3  A: 

What version of grails are you using? It's working fine for both situations for me on 1.0.4 (the latest).

I create a new blank app and created a service class with an integration test:

FooService.groovy:

class FooService {
    def logSomething(message) {
        log.error(message)
        return true
    }
}

FooServiceTests.groovy:

class FooServiceTests extends GroovyTestCase {
    def fooService
    void testSomething() {
    assert fooService.logSomething("it works")
    }
}

When running just test-app, I get the log message:

% grails test-app             

Welcome to Grails 1.0.4 - http://grails.org/
....
-------------------------------------------------------
Running 1 Integration Test...
Running test FooServiceTests...
                        testSomething...[4174] service.FooService it works
SUCCESS
Integration Tests Completed in 440ms
-------------------------------------------------------
...

When running only integration tests, it also works:

% grails test-app -integration

Welcome to Grails 1.0.4 - http://grails.org/
....
-------------------------------------------------------
Running 1 Integration Test...
Running test FooServiceTests...
                    testSomething...[4444] service.FooService it works
SUCCESS
Integration Tests Completed in 481ms
-------------------------------------------------------
....

Are you munging around with the logger class (or overriding any metaclass stuff on the logger, in any previous integration classes or unit tests and not then re-initializing the metaClass?

Ted Naleid
We use Grails 1.0.3, but will switch to 1.0.4 in the next time. I will see, if the update change things.
Mnementh
A: 

I have just had this exact problem and it is very easily resolved by calling the mockLogging functionality:

void testTheMagic() {
    mockLogging(MyMagicService)
    def testService = new MyMagicService()
    ...
}

(using Grails 1.1.1, if it matters)

Scott Bennett-McLeish
A: 

But, all DI tree from testService made by 'new MyMagicService()' should be done manually.

And the service injected doesn't seems to log anything.

kwanghl
A: 

I'm using 1.3.2, and logging in test is working fine. Just be sure that you have specified a log4j configuration for the test environment.

34m0