首页 > 解决方案 > 使用 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,并且我已经在 J​​DK 1.8 和 11 之间来回切换。我已经在这个问题上上下阅读了互联网,现在我没有想法了。任何建议都非常感谢!

标签: scalagradlescalatesthttp4s

解决方案


推荐阅读