1
0
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:
Marcus Holl 2018-07-11 12:29:21 +02:00 committed by GitHub
commit 239e8cc054
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}
}
}
}
}