small medium large xlarge

Back to: All Forums  Grails
Generic-user-small
11 Jan 2010, 23:41
Ray Tayek (10 posts)

seems like the log that was in the tests went away with 1.2. one can use mock logging and use the log from the domain object in a unit test (you do not need to do this in an integration test since the log has been injected by grails.

i wanted a log in the tests, so i added one (please see example below).

thanks

package p import org.apache.log4j.Logger; import grails.test.* class TekEventTests extends GrailsUnitTestCase { protected void setUp() { super.setUp() mockLogging(TekEvent, true) } protected void tearDown() { super.tearDown() } void testMockLog() { new TekEvent().log.error(“mock in unit test”) } void testLog() { log.error(“in unit test”) } void testToString() { def tekEvent = new TekEvent(name: ‘Groovy One’, city: ‘San Francisco, CA’, organizer: [fullName:’John Doe’] as TekUser, venue: ‘Moscone Center’, startDate: new Date(‘6/2/2009’), endDate: new Date(‘6/5/2009’), description: ‘This conference will cover all…’) assertEquals ‘Groovy One, San Francisco, CA’, tekEvent.toString() } static Logger log = Logger.getInstance(this.class) }

package p import grails.test.* import org.apache.log4j.* class TekEventControllerTests extends ControllerUnitTestCase { protected void setUp() { super.setUp() mockLogging(TekEvent,true) }

protected void tearDown() {
	super.tearDown()
}

void testMockLog() {
	new TekEvent().log.error("mock in unit controller test")
}
void testLog() {
	log.error("controller")
}
static Logger log = Logger.getInstance(this.class) }	

package p import org.apache.log4j.Logger;

import grails.test.* class TekEventIntegrationTests extends GrailsUnitTestCase { protected void setUp() { super.setUp() } protected void tearDown() { super.tearDown() } void testPropertiesToSeeIfThereIsALogger() { new TekEvent().properties.each {println it} // see all of the properties that grails injects into your domain class new TekEvent().log.error(“i am a duck”) // should appear on console } void testLog() { log.error(“integration”) } static Logger log = Logger.getInstance(this.class) }

You must be logged in to comment