Search code examples
scalaintellij-ideasbtscalatest

Unable to resolve dependencies from test


I am new to scala. I am trying to create a scala project in IntellIj and adding a test class. I am using the below 2 dependencies in sbt.

libraryDependencies += ("org.scalactic" %% "scalactic" % "3.0.8")
// https://mvnrepository.com/artifact/org.scalatest/scalatest
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test

But I am unable to use the class FunSuite in the test class 'ProcessCSVTest.scala' for testing as it is giving a compilation error.

Although I can see the dependencies in the external library in my IntellIj

enter image description here

Build.sbt file

name := "CSVParser"

version := "0.1"

scalaVersion := "2.13.0"

libraryDependencies += ("org.scalactic" %% "scalactic" % "3.0.8")
// https://mvnrepository.com/artifact/org.scalatest/scalatest
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test

The entire code can be found here - https://github.com/practice09/CSVParser

Can anyone please tell me where I am doing wrong?


Solution

  • One issue is the test ProcessCSVTest.scala is under main sources which means ScalaTest needs to be on the main classpath, however in build.sbt ScalaTest dependency is scoped to the Test classpath

    libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test
    

    So if you remove Test scope like so

    libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8"
    

    then ScalaTest will end up on the main classpath and we can add the following import

    import org.scalatest.FunSuite
    

    However my suggestion is to move the tests out of the main sources and put them under src/test/scala/, and then scope the dependency under Test like before

    libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test
    

    The following command builds an example with correct project structure expected by sbt

    sbt new scala/scala-seed.g8
    

    so try exploring how it is setup and fit your project to match.