【发布时间】:2019-06-15 16:39:21
【问题描述】:
我在模拟另一个方法中正在调用的方法时遇到问题。
例如:在我的主课下面。
class Trial extends TrialTrait {
def run(): String ={
val a = createA()
val b = a.split(" ")
val c = b.size
val d = c + " words are there"
d
}
def createA(): String = {
var a = "above all the things that have been done, one thing remained in silent above all the things that have been done one thing remained in silent above all the that "
a
}
}
下面是我的模拟代码。
class TryMock4 extends FunSuite with BeforeAndAfterEach with MockFactory {
val trial = new Trial
val st = stub[TrialTrait]
test("Mocking the DataFrame") {
val input = "above all the things that have been done, one thing remained in silent above "
(st.createA _).when().returns(input)
val expected = "14 words are there"
val actual = st.run()
Assert.assertEquals(expected,actual)
}
}
我想要做的是将模拟数据传递给createA 并在run 方法中使用它。
但是,它在运行run 方法后给出了null 值。
您能否建议如何实现?
【问题讨论】:
-
感谢@SergGr 的回复。能否请您告诉我如何使用 spy 使用 Mockito 来做到这一点。如果您能提供示例代码,我将不胜感激。
-
Aswanikumar 你读过我链接的文档(第二个链接)吗?你试过吗?
spy的问题到底出在哪里?