1

I'm trying to use Scala IDE. I'm totally new to 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._ is marked by an error:

object scalatest is not a member of package org

I've tried to google it a lot but still don't understand what's wrong. My project structure looks right:

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")

I haven't build the project BTW. Is that could be the reason? I don't know how to build the project from Scala IDE, and if I build it from Windows Command Prompt then it doesn't influence Scala IDE.

Alon
  • 10,381
  • 23
  • 88
  • 152

1 Answers1

4

I assume your project structure is as below,

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

This is simple hello class

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

I created the unit testing as below

import org.scalatest._

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

So, you need just to add scala test in dependcies. Below is simply sbt file

name := "Hello Test"
version := "0.1"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.0.6" % "test")
Moustafa Mahmoud
  • 1,540
  • 13
  • 35