mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
Merge pull request #199 from marcusholl/pr/supportAssertOfLogMessageInCaseOfFailures
Support check for log messages in case of exceptions in code under test
This commit is contained in:
commit
239e8cc054
@ -1,20 +1,33 @@
|
||||
package util
|
||||
|
||||
import com.lesfurets.jenkins.unit.BasePipelineTest
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.rules.TestRule
|
||||
import org.junit.runner.Description
|
||||
import org.junit.runners.model.Statement
|
||||
|
||||
import static org.hamcrest.Matchers.containsString
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.hamcrest.Matchers
|
||||
|
||||
class JenkinsLoggingRule implements TestRule {
|
||||
|
||||
final BasePipelineTest testInstance
|
||||
|
||||
def expected = []
|
||||
|
||||
String log = ""
|
||||
|
||||
JenkinsLoggingRule(BasePipelineTest testInstance) {
|
||||
this.testInstance = testInstance
|
||||
}
|
||||
|
||||
public void expect(String substring) {
|
||||
expected.add(substring)
|
||||
}
|
||||
|
||||
@Override
|
||||
Statement apply(Statement base, Description description) {
|
||||
return statement(base)
|
||||
@ -30,7 +43,30 @@ class JenkinsLoggingRule implements TestRule {
|
||||
log += "$echoInput \n"
|
||||
})
|
||||
|
||||
base.evaluate()
|
||||
Throwable caught
|
||||
|
||||
try {
|
||||
base.evaluate()
|
||||
} catch(Throwable thr) {
|
||||
caught = thr
|
||||
} finally {
|
||||
if(caught instanceof AssertionError) {
|
||||
// Be polite, give other rules the advantage.
|
||||
// We expect other rules located closer to the test case
|
||||
// to throw an AssertionError in case of a violation.
|
||||
throw caught
|
||||
}
|
||||
|
||||
expected.each { substring -> assertThat("Substring '${substring} not contained in log.'",
|
||||
log,
|
||||
containsString(substring)) }
|
||||
|
||||
if(caught != null) {
|
||||
// do not swallow, so that other rules located farer away
|
||||
// to the test case can react
|
||||
throw caught
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user