In my app code it is common that where something goes wrong during app use I want as much information as possible about the circumstances to be logged, including if applicable the stack trace of an Exception which has been thrown.
But during testing I don't want these stack traces to be logged, cluttering up the log file to no purpose.
If this is a situation where the test itself creates an Exception object you can potentially give it a message which can identify it as a dummy Exception, like so:
given:
indexMgr.queryParser = Mock( QueryParser ){
parse(_) >> { throw new Exception( 'dummy parse problem' )}
}
and then in the app code do this:
try {
query = queryParser.parse(queryString)
}catch( e ) {
log.debug( "QP exception msg: $e.message" )
// we don't want a stack trace to be logged if this is a dummy Exception deliberately thrown during testing
if( ! e.message.contains( 'dummy' )) {
// this will log the stack trace of e
log.error( 'query threw Exception in QP.parse()', e )
}
return false
}
... but there are 2 problems with this: firstly it is not always the case that an expected Exception will be created by the testing code, rather than by the app code, and secondly that it feels wrong to be checking for conditions identifying the conduct of a test in the actual app code.
Is there a "best practice" way of tackling this?
Aucun commentaire:
Enregistrer un commentaire