Cannot run 'mvn clean test' using scalatest

233 views Asked by At

I'm learnning how to use ScalaTest.

now I'm using scala 2.11.8, in JetBrains IDEA.

Fisrt I wrote a simple 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.'"
}

then I wrote a test file 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!")
  }
}

when I click the green button it works fine. click green button

But when I run a command mvn clean test,somethings wrong:

[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

and if I delete this TraitASpec.scala ,then run mvn clean testit works fine.

BTW, when I follow the ScalaTest User Guide and wrote a test file like 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()
    }
  }
}

and run mvn clean test it worked.

I guess there is something wrong with the file stucture or code path?

Can you help me? Thank you!

1

There are 1 answers

0
CoolGoose On

In the configuration/Setting check what is the working directory . Set it to the module path.

Also try invalidating /clear cache and Restart, It picks the files again.