简体   繁体   English

assertj中的kotlin扩展功能

[英]kotlin Extension function in assertj

I'm try to implement a extension function in my test with assertj. 我尝试在我的测试中使用assertj实现扩展功能。 I have a custom exception like this: 我有一个这样的自定义异常:

class MyException: Exception {
        constructor(message: String, code: Int) : super(message)
        constructor(cause: Throwable, code: Int) : super(cause)
}

I would like to check the property code in my test. 我想在测试中检查属性code unfortunately we use the java assertj, that's why I tried to implement a extension function. 不幸的是,我们使用了Java assertj,这就是为什么我尝试实现扩展功能的原因。

I have the following, my test: 我的测试如下:

@Test
fun `Creating webdto without name fails`() {
    assertThatExceptionOfType(MyException::class.java)
            .isThrownBy { service.create(WebDto.apply { this.name = null }) }
            .withMessageContaining("Bean validation error.")
            .withErrorCodeContaining(1) // extension function
}

private fun <T : Throwable?> ThrowableAssertAlternative<T>.withErrorCodeContaining(expectedErrorCode: ErrorCode): ThrowableAssertAlternative<T> {
    // How can I access the actual or delegate parameter?
    return this
}

I have no chance to get the actual or delegate parameter in my withErrorCodeContaining 我没有机会在withErrorCodeContaining获取actualdelegate参数

Any ideas? 有任何想法吗? thank you in advance 先感谢您

Not exactly what you're looking for, but will accomplish what you want. 并不是您要找的东西,而是可以实现您想要的东西。

As per docs https://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#exception-assertion , you can use 根据文档https://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#exception-assertion ,您可以使用

val thrown: MyException = (MyException)catchThrowable { 
    service.create(WebDto.apply { this.name = null })
} as MyException

assertThat(thrown.message).isEqualTo("Bean validation error.")
assertThat(thrown.code).isEqualTo(1)
fun <T : MyException?> ThrowableAssertAlternative<T>.withErrorCodeContaining(expectedErrorCode: Int):
    ThrowableAssertAlternative<T> = this.matches({ it?.code == expectedErrorCode },
        "ErrorCode from the RestApiException doesn't match with the expected: <\"$expectedErrorCode\">")

That's the solution I've worked for and expected 这是我一直努力并期望的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改kotlin扩展函数接收器JVM名称 - Changing kotlin extension function receiver JVM name 无法从java调用kotlin扩展函数 - Unable to call kotlin extension function from java 在 Java 代码中使用 kotlin 扩展 function - Using kotlin extension function in Java code 在java代码中定义Kotlin扩展函数 - Define Kotlin extension function in java code 在 Java Optional 的 ifPresent() 中使用 AssertJ 的 assertThat 函数 - Use assertThat function of AssertJ in ifPresent() of Java Optional Kotlin将类型函数作为函数扩展来实现 - 可以从Java调用吗? - Kotlin reified type function as function extension - Callable from Java? 如何在 Java 类的 Kotlin 中声明扩展静态函数? - How to declare an extension static function in Kotlin on Java Classes? Kotlin 1.3.61 无法解析函数文字上扩展属性的返回类型 - Kotlin 1.3.61 fails to resolve return type of extension property on function literal 如何在Kotlin中创建对类进行操作的扩展功能? - How do I create an extension function in Kotlin, which operates on a class? try-with-resources:Kotlin 中的“使用”扩展功能并不总是有效 - try-with-resources: "use" extension function in Kotlin does not always work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM