Scala IDE:对象scalatest不是软件包org

时间:2019-05-06 16:19:16

标签: eclipse scala scala-ide

我正在尝试使用Scala IDE。我是Scala的新手。

HelloSpec.scala:

import org.scalatest._

class HelloSpec extends FlatSpec with Matchers {
  "The Hello object" should "say hello" in {
    Hello.greeting shouldEqual "hello"
  }
}

import org.scalatest._带有错误标记:

  

object scalatest不是org软件包的成员

我已经尝试过很多Google搜索,但仍然不明白这是怎么回事。我的项目结构看起来正确:

src/main/scala:
----Hello.scala
src/test/scala:
----HelloSpec.scala
build.sbt
plugins.sbt

build.sbt:

import Dependencies._

ThisBuild / scalaVersion     := "2.12.8"
ThisBuild / version          := "0.1.0-SNAPSHOT"
ThisBuild / organization     := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
  .settings(
    name := "$name$",
    libraryDependencies += scalaTest % Test
  )

plugins.sbt:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

我还没有建立项目BTW。那可能是原因吗?我不知道如何从Scala IDE构建项目,如果我是从Windows命令提示符构建的,那么它不会影响Scala IDE。

1 个答案:

答案 0 :(得分:4)

我假设您的项目结构如下,

src/
  main/
    resources/
       <files to include in main jar here>
    scala/
       <main Scala sources>
  test/
    resources
       <files to include in test jar here>
    scala/
       <test Scala sources>
build.sbt
plugins.sbt

这是简单的hello类

object Hello {
  def greeting: String = {
    "hello"
  }
}

我创建了如下的单元测试

import org.scalatest._

class HelloSpec extends FlatSpec with Matchers {
  "The Hello object" should "say hello" in {
    Hello.greeting shouldEqual "hello"
  }
}

因此,您只需要在依赖关系中添加scala测试即可。以下是sbt文件

name := "Hello Test"
version := "0.1"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.0.6" % "test")