【问题标题】:scalatest : object scalatest is not a member of package orgscalatest : 对象 scalatest 不是包 org 的成员
【发布时间】:2016-05-31 00:24:54
【问题描述】:

编辑:如果文件在 src/test/scala/tests/ 但不在 src/main/scala/mypackage/ 中,它可以工作 为什么?

我尝试了一些主题的解决方案,人们遇到了几乎相同的问题,但没有一个有效。

详细来说,我在 build.sbt 中有这个:

libraryDependencies ++= Seq(
   ...
  "org.scalatest" % "scalatest_2.10" % "2.2.1" % "test",
   ...

在 intellij 中有一个类:

import org.scalatest.{BeforeAndAfterAll, Suite}

{BeforeAndAfterAll, Suite} 为红色,所以我猜找到了 scalatest

sbt 包也不行:

object scalatest 不是包 org [error] import 的成员 org.scalatest。 {BeforeAndAfterAll, Suite}

我已经试过了:

  • sbt 干净更新
  • 重启 + 使 intellij 缓存无效
  • 删除 .idea/ 并重新导入
  • libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.0" % "test" 代替 ogf 实际
  • 键盘上的魔法仪式

没有任何作用

有什么想法吗?

【问题讨论】:

  • 为什么要在 src/main 中使用 scalatest?
  • 从 scalatest 库依赖项中删除 % "test"。只要存在,您就不会在编译阶段在类路径中获得所需的条目。
  • 你解决了这个问题吗?我遇到了同样的错误。任何建议都会有所帮助。
  • 不,对不起,斯里尼瓦斯
  • IntelliJ 中的 Maven 遇到了这个问题。我使缓存无效并重新启动,但没有帮助。我重新导入了 Maven 并修复了它。

标签: scala intellij-idea sbt scalatest


【解决方案1】:

你应该看看sbt project structure

src/
  main/
    resources/
       <files to include in main jar here>
    scala/
       <main Scala sources>
    java/
       <main Java sources>
  test/
    resources
       <files to include in test jar here>
    scala/
       <test Scala sources>
    java/
       <test Java sources>

您的 scala 测试应在 src/test/scala/ 下进行。

如果您真的想在 main 中使用 scalatest(并且您知道自己要做什么),请尝试以下操作:

  "org.scalatest" % "scalatest_2.10" % "2.2.1",

在您原来的build.sbt 中,“test”是配置,这意味着 scalatest 只会在测试类路径上,主要来源不需要它。这通常是库的好习惯,因为您的用户通常不需要您的测试依赖项来使用您的库 (ref)。

【讨论】:

  • 在 IntelliJ 中从 package view 切换到 project view 一秒钟,然后你看到了痛苦;)谢谢你的帮助!
猜你喜欢
  • 2012-12-20
  • 2019-09-24
  • 2021-05-24
  • 2021-08-29
  • 2016-02-29
  • 2019-11-22
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
相关资源
最近更新 更多