我正在学习如何使用 ScalaTest。
现在我在 JetBrains IDEA 中使用 scala 2.11.8。
首先我写了一个简单的 trait 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()
}
}
}
并运行mvn clean test
它。
我猜文件结构或代码路径有问题?
你能帮助我吗?谢谢!