gpt4 book ai didi

java - 在用于测试 Junit4 的函数中模拟私有(private)变量

转载 作者:行者123 更新时间:2023-12-02 12:46:14 24 4
gpt4 key购买 nike

我正在为一个函数编写一个 Junit4 测试,该函数具有基于其他函数设置的 boolean 变量。如何在我的测试用例中模拟这些 boolean 变量。

我正在测试的功能

private var operatorSigned = false
private var engineerSigned = false


override fun onValidationSucceeded() {
if (operatorSigned && engineerSigned) {
sendQualityControlCheck()
} else {
view.showToastMessage(app.getString(R.string.quality_control_signatures_not_complete))
}
}

这就是设置值的方式
override fun signatureSigned(name: String, svgSignature: String, bitmapSignature: Bitmap, signatureType: SignatureType) {
val svgSignatureGzipped = CompressionTools.gzipCompress(svgSignature)
when (signatureType) {
SignatureType.OPERATOR -> {
operatorSignature = QualityControlSignature(svgSignatureGzipped, DateTime())
operatorSigned = true
view.operatorSignatureSigned()
}
SignatureType.ENGINEER -> {
engineerSignature = QualityControlSignature(svgSignatureGzipped, DateTime())
engineerSigned = true
engineerName = name
view.engineerSignatureSigned()
}
}
}

我的测试用例
@Test
fun `must show toast message when onValidationSucceeded is called and operatorSigned is false`() {
`when`(app.getString(R.string.quality_control_signatures_not_complete)).thenReturn("Operator and Engineer signature required")
presenter.onValidationSucceeded()
verify(view).showToastMessage("Operator and Engineer signature required")
}

我希望能够更改 operatorSigned 和 EngineerSigned 的值我该怎么做。

最佳答案

让我们假装OPERATOR您的代码路径将这些 boolean 值设置为 false:

@Test
fun `must show toast message when onValidationSucceeded is called and signature is Operator`() {
// Given
signatureSigned("unusedName", "unusedSig", null, SignatureType.OPERATOR)
`when`(app.getString(R.string.quality_control_signatures_not_complete)).thenReturn("Operator and Engineer signature required")
// When
presenter.onValidationSucceeded()
// Then
verify(view).showToastMessage("Operator and Engineer signature required")
}

(不得不假装,因为现在您共享的代码只会将 boolean 值设置为 true)。

您现在正在对类行为进行单元测试,而不是测试该行为的编码方式。例如,现在如果那些 boolean 值被重构为 Int 值。该测试仍然可以通过,无需您触摸它, 这是好事 !

或者,如果您的代码保持原样。你可以这样测试:
@Test
fun `must show toast message when onValidationSucceeded is called and has not been signature signed`() {
// Given
// We haven't signature signed (see verify)
`when`(app.getString(R.string.quality_control_signatures_not_complete)).thenReturn("Operator and Engineer signature required")
// When
presenter.onValidationSucceeded()
// Then
verify(view).showToastMessage("Operator and Engineer signature required")
verify(view, never()).operatorSignatureSigned()
verify(view, never()).engineerSignatureSigned()
}

如何模拟一个静态方法(提示你不要,你把它移到你系统的边缘,然后再担心它)。

你现在的类(class):
class MyClass {

override fun signatureSigned(name: String, svgSignature: String, bitmapSignature: Bitmap, signatureType: SignatureType) {
val svgSignatureGzipped = CompressionTools.gzipCompress(svgSignature)
...
}

}

因此,您需要删除静态调用,以获取另一个可以将静态调用作为依赖项的类。
interface SignatureCompressor {
fun compress(signature: String)
}

class GZipSignatureCompressor : SignatureCompressor {
override fun compress(signature: String) {
return CompressionTools.gzipCompress(signature)
}
}

现在你的类(class)变成:
class MyClass(private val compressor: SignatureCompressor) {

override fun signatureSigned(name: String, svgSignature: String, bitmapSignature: Bitmap, signatureType: SignatureType) {
val svgSignatureGzipped = compressor.compress(svgSignature)
...
}

}

你可以模拟压缩机:
 val cut = MyClass(mockk())

如果您只有一种压缩 的实现,您也不需要该接口(interface)。

关于java - 在用于测试 Junit4 的函数中模拟私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60211478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com