scala - 使用 scalatest 对 http4s 进行单元测试
问题描述
o/
我正在尝试在 Scala 中为 cex.io 的 API 实现一个客户端。因为我喜欢函数式编程(或者我认为我喜欢),所以我尝试通过使用 http4s 库来做到这一点。这是一些实验代码:
class CexApi {
val baseUrl: String = "https://cex.io/api/"
private val blockingEC = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(5))
private implicit val cs: ContextShift[IO] = IO.contextShift(blockingEC)
private val httpClient: Client[IO] = JavaNetClientBuilder[IO](blockingEC).create
def testCall(): IO[String] = {
httpClient.expect[String](baseUrl + "currency_limits")
}
}
现在我想测试所说的客户:
class CexApiSpec extends AnyFlatSpec {
"The Api" should "be able to run a simple method" in {
val api = new CexApi
val result = api.testCall()
assert(result.toString() != null)
}
}
不幸的是,直接在 IntelliJ 中运行测试总是会给我:
A needed class was not found. This could be due to an error in your runpath. Missing class: scala/Serializable
java.lang.NoClassDefFoundError: scala/Serializable
调试到测试表明错误发生在实例化 api 类 ( val api = new CexApi
) 时。
该项目是用 Gradle 构建的,因为我想在我正在做的事情的同时使用 Spring Boot。运行 gradle 测试任务时,它声明它根本没有收到任何测试事件。
我正在使用 Scala 2.13、ScalaTest 2.13:3.1.0 http4s 0.20.15、Gradle 6.0.1,并且我已经在 JDK 1.8 和 11 之间来回切换。我已经在这个问题上上下阅读了互联网,现在我没有想法了。任何建议都非常感谢!
解决方案
推荐阅读
- c# - 无法从 WPF 数据网格更新 SQL
- python - Ubuntu Groovy Gorilla 中 Geany 的问题
- node.js - Puppeteer [错误:执行上下文被破坏,很可能是因为导航。]
- c++ - 在 ubuntu mingw 上指定线程模型(如果可能)
- angular - 单击按钮时组件不会重新加载
- python - 如何从 json 元素快速创建二维 numpy 数组?
- r - R markdown:将数据框中的值报告为文本
- python - Ubuntu 20.04 中的语音识别模块 [python] 出现问题
- xpath - 如何获取特定的 xpath 标记值
- python - TicTacToe Tkinter GUI 不断崩溃