So I have a class that has a method that logs a message:
class Car {
private Logger logger = LoggerFactory.getLogger(Car.class);
void startCar() {
logger.error("car stopped working");
}
}
How can I test that the error was logged using the spock testing framework?
class CarTest extends Specification {
def "test startCar"() {
given:
Car newCar = new Car();
when:
newCar.startCar();
then:
// HOW CAN I ASSERT THAT THE MESSAGE WAS LOGGED???
}
}
you could check for an invocation of error on the logger
edit: Full example https://github.com/christoph-frick/spock-test-logging