简体   繁体   English

将单元测试添加到 IntelliJ 中的旧 Scala 项目

[英]Adding unit test to a legacy Scala project in IntelliJ

I have a legacy Scala project to work with using maven as build tool.我有一个遗留的 Scala 项目可以使用 maven 作为构建工具。

Initially there is core code only under folder structure src/main/scala/ with package xyz and a file Main.scala with code as:最初只有在文件夹结构 src/main/scala/ 下有核心代码,包 xyz 和文件 Main.scala ,代码为:

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.所以我在src下手动添加了一个文件夹结构作为test/scala。 Then I copied the package as for core code, ie xyz and added MainTest.scala with test code as:然后我复制了核心代码的包,即 xyz 并添加了 MainTest.scala 和测试代码:

class MainTest extends FunSuite {

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

 }

Running test gives error as:运行测试给出错误为:

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

assert(Main.cube(3) === 27)断言(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对于下面的项目结构 MainTest.scala 和 Main.scala 在同一个包 xyz 中,因此不需要包导入,但是 MainTempTest.scala 和 Main.scala 在不同的包中,因此必须进行显式导入 import xyzMain

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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