简体   繁体   中英

Adding unit test to a legacy Scala project in IntelliJ

I have a legacy Scala project to work with using maven as build tool.

Initially there is core code only under folder structure src/main/scala/ with package xyz and a file Main.scala with code as:

object Main {
 def cube(x: Int) = {
   x * x * x
 }
}

There are no tests for the code. So I manually added a folder structure as test/scala under src. Then I copied the package as for core code, ie xyz and added MainTest.scala with test code as:

class MainTest extends FunSuite {

   test("Main.cube") {
     assert(Main.cube(3) === 27)
   }

 }

Running test gives error as:

Error:(8, 12) not found: value Main

assert(Main.cube(3) === 27)

Why do I get this error?

For below project structure MainTest.scala and Main.scala are in the same package xyz therefore no package import is required, However MainTempTest.scala and Main.scala are in different package therefore an explicit import has to be done import xyzMain

src
  -main
    --scala
      ---x.y.z
        ----Main.scala
  -test
    --scala
      ---MainTempTest.scala
      ---x.y.z
        ----MainTest.scala

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM