How to unit test logging error with Spock framework in groovy -



How to unit test logging error with Spock framework in groovy -

so have class has method logs message:

class auto { private logger logger = loggerfactory.getlogger(car.class); void startcar() { logger.error("car stopped working"); } }

how can test error logged using spock testing framework?

class cartest extends specification { def "test startcar"() { given: auto newcar = new car(); when: newcar.startcar(); then: // how can assert message logged??? } }

you check invocation of error on logger

@grab(group='org.spockframework', module='spock-core', version='0.7-groovy-2.0') @grab(group='org.slf4j', module='slf4j-api', version='1.7.7') @grab(group='ch.qos.logback', module='logback-classic', version='1.1.2') import org.slf4j.logger class mocklog extends spock.lang.specification { public class auto { private logger logger = org.slf4j.loggerfactory.getlogger(car.class); void startcar() { logger.error('car stopped working'); } } def "mock log"() { given: def auto = new car() car.logger = mock(logger) when: car.startcar() then: 1 * car.logger.error('car stopped working') } }

unit-testing logging groovy spock

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -