【发布时间】:2019-10-14 12:08:55
【问题描述】:
是否可以使用版本 2.11 和 scalatest 3.0.4 替换仅用于 Scala 中特定单元测试的方法?
这是我的场景: 我有方法
import java.security.SecureRandom
import org.bouncycastle.util.encoders.Hex
class KeyHasher extends Serializable {
// getSalt returns a random number that servers as salt for the actual hash algorithm
def getSalt(length): String = {
val salt: Array[Byte] = new Array[Byte](length)
secureRandom.nextBytes(salt)
Hex.toHexString(hashedKey)
}
def getEncodedKey(actualKey: String): String = {
...
val salt = getSalt()
...
return applyHash(salt) // apply a hash algorithm using a salt
}
}
对于特定的单元测试,我想确保方法 getSalt 返回一个常量,而不是随机数。
我知道,我可以在 getEncodedKey 方法中使用额外的输入参数 (testFlag) 或在 getSalt 方法中使用全局变量来获得解决方案。但是,我想知道是否有一种方法不接触 getEncodedKey 并且只在一个单元测试中实现某些东西。
【问题讨论】:
-
你能显示更多代码吗?
getSalt来自哪里?是不是和getEncodedKey同班?