【问题标题】:Unit testing trait with object带有对象的单元测试特征
【发布时间】:2015-01-07 12:30:02
【问题描述】:

我有以下构造,我有一个

trait DataServiceLocalImpl extends DataService {
  override lazy val dataService = DataComponentLocalImpl
}

object DataComponentLocalImpl extends DataComponent {
  def getData(element:String):String = GetStuffFromFile(element)
}

trait DataService {
  val dataService: DataComponent
}

trait DataComponent {
  def getData(element:String):String
}

GetStuffFromFile 从磁盘读取文件一次(我只想要一次,因此对象),创建一个映射,然后返回元素的值。

这一切都是在 Play Framework 2.3 环境中完成的,并且该应用程序也可以正常工作,但是当我在测试中隐式使用它时,我收到以下错误:

java.lang.NoClassDefFoundError: Could not initialize class DataComponentLocalImpl

测试套件:

class AutoCompleteSpec extends PlaySpec with Mockito with OneAppPerSuite {
  val resource = new DataServiceLocalImpl {}
  implicit val dataService = resource.dataService
}

如果我删除隐式它会起作用......

【问题讨论】:

    标签: scala unit-testing playframework-2.0


    【解决方案1】:

    您应该创建一个覆盖服务的对象。

      object FakeImpl extends DataServiceLocalImpl {
        override dataService = //Fake or test data service here
      }
    

    然后您创建一个匿名类定义,允许您测试特征。

    【讨论】:

    • 你可以在scala中扩展一个对象吗?
    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多