I was wondering, in Kotlin, is there the possibility to call the equivalent of the java method:
assertEquals(double expected, double actual, double precision)
because everytime I'm getting this method instead
assertEquals(expected: T, actual: T, message: String)
And i can't find the one with the precision parameter. Calling the Java one should be fine too I guess.
My call to the method:
assertEquals(5000.00, calculateCouponAmount(basicFaceValue, basicInterestRate, amortizationBullet, couponNumber1), 0.01)
I'm getting an error because 0.01 gets in the "message" field
I have figured it out!
This is how it's done
import org.junit.*
import Kotlin.Test.assertEquals
Assert.assertEquals(expected, actual, precision) // to use the jUnit standard one
assertEquals(expected, actual, message) // to use the Kotlin one
I couldn't find one, so I created my own set of functions:
import java.math.BigDecimal
import kotlin.math.abs
import kotlin.test.asserter
internal fun assertEquals(expected: Double, actual: Double?, epsilon: Double, message: String? = null) {
asserter.assertNotNull(null, actual)
asserter.assertTrue({ messagePrefix(message) + "Expected <$expected>, actual <$actual>, should differ no more than <$epsilon>." },
abs(expected - actual!!) <= epsilon)
}
internal fun assertEquals(expected: BigDecimal, actual: BigDecimal?, epsilon: Double, message: String? = null) {
asserter.assertNotNull(null, actual)
asserter.assertTrue({ messagePrefix(message) + "Expected <$expected>, actual <$actual>, should differ no more than <$epsilon>." },
(expected - actual!!).abs().toDouble() <= epsilon)
}
internal fun assertEquals(expected: Double, actual: BigDecimal?, epsilon: Double, message: String? = null) {
asserter.assertNotNull(null, actual)
asserter.assertTrue({ messagePrefix(message) + "Expected <$expected>, actual <$actual>, should differ no more than <$epsilon>." },
abs(expected - actual!!.toDouble()) <= epsilon)
}
internal fun messagePrefix(message: String?) = if (message == null) "" else "$message. "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With