无法使用 scalatest 运行"mvn 清洁测试"



我正在学习如何使用ScalaTest。

现在我在JetBrains IDEA中使用scala 2.11.8。

Fisrt我写了一个简单的特质traitA

package Cha1_TraitsAndMixinCompositions.Clash
trait A {
def hello(): String = "Hello, I am trait A!"
def pass(a: Int): String = s"Trait A said: 'You passed $a.'"
}

然后我写了一个测试文件TraitASpec.scala:

import Cha1_TraitsAndMixinCompositions.Clash.A
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class TraitASpec extends AnyFlatSpec with Matchers with A {
"hello" should "greet properly." in {
hello() should equal("Hello, I am trait A!")
}
}

当我点击绿色按钮时,它工作正常。点击绿色按钮

但当我运行命令mvn clean test时,出现了问题:

[INFO] compiling 4 Scala sources to /Users/yangdaichuan/Desktop/gitlab/SparkGraphX/target/test-classes ...
[ERROR] /Users/yyy/Desktop/gitlab/SparkGraphX/src/test/scala/TraitASpec.scala:1: not found: object Cha1_TraitsAndMixinCompositions
[ERROR] /Users/yyy/Desktop/gitlab/SparkGraphX/src/test/scala/TraitASpec.scala:5: not found: type A
[ERROR] /Users/yyy/Desktop/gitlab/SparkGraphX/src/test/scala/TraitASpec.scala:7: not found: value hello
[ERROR] three errors found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  18.812 s
[INFO] Finished at: 2022-03-02T14:13:23+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:4.5.6:testCompile (default) on project Spark2Learn: Execution default of goal net.alchim31.maven:scala-maven-plugin:4.5.6:testCompile failed: Compilation failed: InterfaceCompileFailed -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

如果我删除这个TraitASpec.scala,然后运行mvn clean test,它会正常工作。

顺便说一句,当我遵循ScalaTest用户指南并编写了一个类似StackSpec.scala:的测试文件时

import collection.mutable.Stack
import org.scalatest.flatspec.AnyFlatSpec
class StackSpec extends AnyFlatSpec {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
assert(stack.pop() === 2)
assert(stack.pop() === 1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[String]
assertThrows[NoSuchElementException] {
emptyStack.pop()
}
}
}

并运行CCD_ 7。

我想是文件结构或代码路径有问题吗?

你能帮我吗?非常感谢。

在配置/设置中检查什么是工作目录。将其设置为模块路径。

同时尝试无效/清除缓存并重新启动,它会再次拾取文件。

最新更新