scala 中的模拟案例类:Mockito [英] mock case class in scala : Mockito
问题描述
在我的播放应用程序中,我打算模拟一个案例类.我能够这样做,但它创建了一个所有成员变量为空的对象.有没有办法创建案例类的模拟对象,以便对象可以初始化一些成员?
In my play application i intend to mock a case class. I am able to do so but it creates an object with all member variables null. Is there a way to create mock objects of a case classes such that the object can have some members initialized?
case class User(name: String, address: String)
val mockUser = mock[User]
user.name // null
user.address //null
如何创建一个模拟用户,以便我可以为姓名和地址分配一些值?
how do i create a mockUser such that i can assign some values to name and address?
我需要能够模拟对象,因为我想拥有成员方法之一的预定义行为.(此成员方法调用外部服务,我不希望在进行单元测试时调用外部服务.)在另一个成员方法中调用该成员方法,我想测试该方法.
I need the ability to mock the object because i want to have predefined behavior of one of the member method. (This member method calls an external service and i dont want the external service call while doing a unit test.) The member method is called inside another member method, which i want to test.
推荐答案
您永远不需要模拟案例类.这就像模拟一个整数".
You should never need to mock case classes. It's like "mocking an integer".
val mockUser = User("mockName", "mockAddress")
有什么问题?
这篇关于scala 中的模拟案例类:Mockito的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!