我觉得这很难解释,但会尽量保持简单 我正在编写一个单元测试,其中在另一个类中访问了一个函数(getQualityControlCheckDataForUplift()),但是它确实进入了该函数并执行了应做的事情,它直接返回了return语句。
这是功能。我应该嘲笑QualityControlCheck
的{{1}}
我已经简化了代码,以使其更加清晰易懂
在我的测试案例中,
data class
访问getQualityControlCheckDataForUplift()
showQualityControlChecks()
,不确定原因任何建议都会非常有帮助 我真的很抱歉,如果代码不容易理解
getQualityControlCheckDataForUplift()
我的测试用例
fun getQualityControlCheckDataForUplift(qualityControlMilestone: QualityControlMilestone, uplift: Float):
ArrayList<QualityControlCheck>? {
val qcChecksForUplift: ArrayList<QualityControlCheck>? = ArrayList()
val qcChecksForMilestone = mapOfQCChecksForMilestone[qualityControlMilestone.milestoneText]
qcChecksForMilestone?.forEach {
if (it.uplift == uplift) qcChecksForUplift?.add(it)
}
return qcChecksForUplift
}
OrderStateMonitor
@RunWith(MockitoJUnitRunner::class)
class OrderStateMonitorTest : BaseTest() {
@InjectMocks
private lateinit var orderStateMonitor: OrderStateMonitor
@Mock
private lateinit var qualityControlCheckRequiredDuringFuellingBus: QualityControlCheckRequiredDuringFuellingBus
@Mock
private lateinit var qualityControlChecksDataProvider: QualityControlChecksDataProvider
@Before
fun setUp() {
qualityControlChecksDataProvider = QualityControlChecksDataProvider(offlineDataStorage, app)
}
@Test
fun `upliftedVolumeUpdated abcdefg`() {
qualityControlChecksDataProvider.loadMapWithQCChecksForMilestone()
every(fuelOrderValuesProvider.upliftedVolume()).thenReturn(1000.0f)
orderStateMonitor.upliftedVolumeUpdated()
verify(qualityControlCheckRequiredDuringFuellingBus, never()).postUnavailable()
verify(qualityControlCheckRequiredDuringFuellingBus, once()).postAvailable(qualityControlChecks)
}
}
QualityControlChecksDataProvider
class OrderStateMonitor @Inject constructor(
private val orderStorage: CurrentOrderStorage,
private val fuelOrderValuesProvider: FuelOrderValuesProvider,
private val noServiceRequiredAvailabilityBus: NoServiceRequiredAvailabilityBus,
private val qualityControlCheckRequiredDuringFuellingBus: QualityControlCheckRequiredDuringFuellingBus,
private val qualityControlChecksDataProvider: QualityControlChecksDataProvider
) {
fun upliftedVolumeUpdated() {
showQualityControlChecks()
}
private fun showQualityControlChecks() {
// WHEN I TRY TO ACCESS ` //qualityControlChecksDataProvider.getQualityControlCheckDataForUplift` IT DOES NOT GET INTO THAT FUNCTION
val qualityControlChecks =
qualityControlChecksDataProvider.getQualityControlCheckDataForUplift(
QualityControlMilestone.FUELLING, 1000.0f)
}
}
答案 0 :(得分:0)
由于您嘲笑了类而未执行该方法
@RunWith(MockitoJUnitRunner::class)
class OrderStateMonitorTest : BaseTest() {
@InjectMocks
private lateinit var orderStateMonitor: OrderStateMonitor
@Mock
private lateinit var qualityControlCheckRequiredDuringFuellingBus: QualityControlCheckRequiredDuringFuellingBus
private lateinit var qualityControlChecksDataProvider: QualityControlChecksDataProvider
@Before
fun setUp() {
qualityControlChecksDataProvider = spy(QualityControlChecksDataProvider(offlineDataStorage, app))
}
@Test
fun `upliftedVolumeUpdated abcdefg`() {
qualityControlChecksDataProvider.loadMapWithQCChecksForMilestone()
every(fuelOrderValuesProvider.upliftedVolume()).thenReturn(1000.0f)
orderStateMonitor.upliftedVolumeUpdated()
verify(qualityControlCheckRequiredDuringFuellingBus, never()).postUnavailable()
verify(qualityControlCheckRequiredDuringFuellingBus, once()).postAvailable(qualityControlChecks)
}
}
或使用CALL_REAL_METHOD
@RunWith(MockitoJUnitRunner::class)
class OrderStateMonitorTest : BaseTest() {
@InjectMocks
private lateinit var orderStateMonitor: OrderStateMonitor
@Mock
private lateinit var qualityControlCheckRequiredDuringFuellingBus: QualityControlCheckRequiredDuringFuellingBus
private lateinit var qualityControlChecksDataProvider: QualityControlChecksDataProvider
// java syntax
@before
public void setUp(){
qualityControlChecksDataProvider = mock(QualityControlChecksDataProvider, Mocktio.CALL_REAL_METHOD);
}
@Test
fun `upliftedVolumeUpdated abcdefg`() {
qualityControlChecksDataProvider.loadMapWithQCChecksForMilestone()
every(fuelOrderValuesProvider.upliftedVolume()).thenReturn(1000.0f)
orderStateMonitor.upliftedVolumeUpdated()
verify(qualityControlCheckRequiredDuringFuellingBus, never()).postUnavailable()
verify(qualityControlCheckRequiredDuringFuellingBus, once()).postAvailable(qualityControlChecks)
}
}