All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.vendigo.acetest.assertion.ExceptionMatcher.kt Maven / Gradle / Ivy

package com.github.vendigo.acetest.assertion

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.notNullValue
import java.io.PrintWriter
import java.io.StringWriter

private val LINE_SEPARATOR = "\n"
private val CAUSED_BY = "Caused by: "

fun matchException(throwable: Throwable?, stackTraceContains: String) {
    assertThat(throwable, notNullValue())
    val stringWriter = StringWriter()
    throwable!!.printStackTrace(PrintWriter(stringWriter))
    val stackTrace = stringWriter.toString()
    val exceptionString = stackTrace.split(LINE_SEPARATOR.toRegex())
            .firstOrNull { s -> s.contains(CAUSED_BY) }
    if (exceptionString != null) {
        assertThat(exceptionString, containsString(stackTraceContains))
    } else {
        throw AssertionError("Stacktrace doesn't contain 'Caused by' section")
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy