Posts Tagged ‘unit testing’

Quick note on groovy testing a spock

February 29, 2016 Leave a comment

Been getting to grips with gradle, groovy and spock, on a little project and couldnt figure out how to get the GroovyTestCase power assertions available to spock tests.

So eventually found it you have to add the following import

import static groovy.test.GroovyAssert.*
import static org.assertj.core.api.Assertions.*

this enables you to use the the groovy power asserts (shouldfail, assertFalse, etc ), the other is a fluent assertion library that has some interesting features – you need to update your compile dependencies to include ‘org.assertj:assertj-core:3.0.0’

I have not played with the Hamcrest matchers at this point but probably worth a look as these with Spock too

If you want Gradle to find and run your tests i’ve found its easiest to end your class file nameTest.groovy then you can run single shot tests for example like this

gradle -Dtest.single=DynamicRuleSpecification test

which will run the DynamicRuleSpecificationTest.groovy in your ‘src/test/groovy’ folder

Happy testing

Categories: gradle, Groovy, Testing Tags: ,

How to get Grails log output in unit testing

February 6, 2014 Leave a comment

Spent some time struggling with getting grails to produce output from logging when running unit and integration tests

read the documentation and tried various stuff to no avail. Then i spotted something silly i’d done (i had def setup () and def teardown () methods – which don’t get called. They needed to be void setUp(), and void tearDown())

having fixed this my code started to get called.

grails injects a log property into unit tests which is of type java.util.logging.Logger. In the GGTS console this prints out in red.

if you don’t want the injected log you can get one directly using one of the logger factories (either apache, or sl4j) like this

package com.softwood

import grails.test.mixin.*
import grails.test.mixin.domain.DomainClassUnitTestMixin

import org.neo4j.graphdb.GraphDatabaseService

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.apache.commons.logging.LogFactory

class DomainTests {
	GraphDatabaseService gdb
	static Logger sllog = LoggerFactory.getLogger(DomainTests)
	def alog = LogFactory.getLog(getClass())
	void setUp () { "java util: hello William : " + log.class 
		assert sllog.isInfoEnabled() "sl4j: getting gdb bean : " + sllog.class "apache: getting gdb bean : " + alog.class

you’ll notice the assert on one of the loggers – this is checking that Info logging is enabled.

you control that by ensuring that the log4j section in grails-app/conf/config.groovy is set correctly. This is described in section 4.1.2 of the grails users guide.

you have to explicitly set the level in order to see the output. The logger will print from the level and any above. My default configuration from the project create didn’t include any “Info” logging so i added the last line – info ‘com.softwood’

if you remove this – then the assert previously noted will fail

I didn’t need to uncomment out the root section – though you can, it sets the default behavior for the root logger that the others inherit

// log4j configuration
log4j = {
    // Example of changing the log pattern for the default console appender:
    appenders {
        console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
	//set default root logger behaviour for the system - use info and send to console appender
	root {
		error    'stdout'
		additivity = true
	} */

    error  'org.codehaus.groovy.grails.web.servlet',        // controllers
           'org.codehaus.groovy.grails.web.pages',          // GSP
           'org.codehaus.groovy.grails.web.sitemesh',       // layouts
           'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
           'org.codehaus.groovy.grails.web.mapping',        // URL mapping
           'org.codehaus.groovy.grails.commons',            // core / classloading
           'org.codehaus.groovy.grails.plugins',            // plugins
           'org.codehaus.groovy.grails.orm.hibernate',      // hibernate integration
	info   'com.softwood' 

having done this when you now run your test – and i didn’t need to -echoOut to the test-app -unit , you’ll see your output print to the console

the explicit loggers generate black text output – and have slightly less decoration than the default injected log

|Configuring classpath
|Environment set to test
|Compiling 1 source files
|Running 1 unit test...
|Running 1 unit test... 1 of 1Feb 06, 2014 10:49:24 PM java_util_logging_Logger$info call
INFO: hello William : class java.util.logging.Logger

softwood.DomainTests getting gdb bean : class org.slf4j.impl.GrailsLog4jLoggerAdapter
softwood.DomainTests a: getting gdb bean : class org.apache.commons.logging.impl.SLF4JLog
|Completed 1 unit test, 0 failed in 0m 3s
|Tests PASSED - view reports in C:\Users\802518659\Documents\temp (not backed up)\grails 3.3 workspace\TestNeo4jGrails235\target\test-reports

so now we are back on track and you can see some output when you run your unit tests – not just whether the asserts pass or fail quietly

note: when you do an integration test the log that’s injected by Grials by default is now of type class org.apache.commons.logging.impl.SLF4JLog

also integration tests don’t use the unit test mixins so you just extend your test class from GroovyTestCase.

good testing …

%d bloggers like this: