is there an equivalent to assertJ returns and asse...
# kotest
s
is there an equivalent to assertJ returns and assertK prop in kotest-assertions? So far I only found hints, but they do not work with junit right? I like having my assertion library have the information which field is asserted so it has nice error messages, but I want to have this automated so I do not have to repeat the fieldname twice
Copy code
import assertk.all
import assertk.assertions.isEqualTo
import assertk.assertions.prop
import org.junit.jupiter.api.Test

class Test {
    data class Data(val id: Int)

    @Test
    internal fun test() {
        // AssertJ
        org.assertj.core.api.Assertions.assertThat(Data(1)).returns(1, Data::id)
        
        // AssertK
        assertk.assertThat(Data(1)).all {
            prop(Data::id).isEqualTo(1)
        }
        
        // kotest-assertions?
    }
}
e
assertSoftly(Data(1)) { id shouldBe 1 }
?
s
@Emil Kantis thanks for your suggestion. I am looking for an alternative with a more descript error message though.
Copy code
import io.kotest.assertions.assertSoftly
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class Test {
    data class Data(val id: Int)

    @Test
    internal fun test() {
        assertSoftly { Data(1).id shouldBe 2 }
    }
}
Produces: expected:<2> but was:<1> Expected :2 Actual :1 I am looking for a message that includes what was tested like Data.id was 1, should be 2
e
Gotcha. How about:
Copy code
Data(1) shouldBeEqualToComparingFields Data(2)
Results in:
Copy code
java.lang.AssertionError: Expected Data(id=1) to equal Data(id=2)
 Using fields: id
 Value differ at:
 1) id:
		expected:<2> but was:<1>
There's also
Copy code
Data(1).shouldBeEqualToUsingFields(Data(2), Data::id) ->

Data(id=1) should be equal to Data(id=2) using fields [id]; Failed for [id: 1 != 2]
java.lang.AssertionError: Data(id=1) should be equal to Data(id=2) using fields [id]; Failed for [id: 1 != 2]
s
I see, shouldBeEqualToComparingFields works for me 😄
177 Views