我想知道,在Kotlin中,是否有可能调用相当于java方法:
assertEquals(double expected, double actual, double precision)
因为我每次都会使用此方法
assertEquals(expected: T, actual: T, message: String)
我无法找到具有精度参数的那个。 我猜这个调用Java也应该没问题。
我对方法的调用:
assertEquals(5000.00, calculateCouponAmount(basicFaceValue, basicInterestRate, amortizationBullet, couponNumber1), 0.01)
我收到错误,因为0.01收到"消息"字段
答案 0 :(得分:4)
我已经弄清楚了!
这就是它的完成方式
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
答案 1 :(得分:3)
我找不到一个,所以我创建了自己的一组函数:
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. "
答案 2 :(得分:2)
只需检查以确保您导入了具有所需assertEquals
方法的正确类。除此之外,您可能需要验证calculateCouponAmount()
方法是否返回Double
。