关于scala:在IntelliJ中运行ScalaTest时“无法加载套件类”

'Unable to load a Suite class' while running ScalaTest in IntelliJ

我今天在Intellij中运行一个简单的TestKit测试时遇到了一些问题。 测试针对Scala代码(我具有Intellij的Scala插件),并且基于Ray Roestenburg的示例。

Intellij项目是使用" Maven模块"创建的,然后我将所有依赖项添加到该项目并创建了我的项目。 测试位于以下位置:

ActorBlast/src/test/scala/basicTest.scala

我基本上是在测试上"右键单击",然后选择"运行"。 我得到的是以下错误:

"C:\Program Files\Java\jdk1.7.0_25\bin\java" -Didea.launcher.port=7540...
Testing started at 2:29 PM ...
Unable to load a Suite class. This could be due to an error in your runpath.

Missing class: BasicActorSpec java.lang.ClassNotFoundException:
BasicActorSpec at
java.net.URLClassLoader$1.run(URLClassLoader.java:366) at
java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
org.scalatest.tools.Runner$$anonfun$35.apply(Runner.scala:2393) at
org.scalatest.tools.Runner$$anonfun$35.apply(Runner.scala:2391) at
scala.collection.TraversableLike$$anonfun$filter$1.apply(TraversableLike.scala:264)
at scala.collection.immutable.List.foreach(List.scala:318) at
scala.collection.TraversableLike$class.filter(TraversableLike.scala:263)
at scala.collection.AbstractTraversable.filter(Traversable.scala:105)
at
org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:2391)
at
org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1006)
at
org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1005)
at
org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:2659)
at
org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1005)
at org.scalatest.tools.Runner$.run(Runner.scala:845) at
org.scalatest.tools.Runner.run(Runner.scala) at
org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:144)
at
org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606) at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 0

我不知道这意味着什么。 我已经做了很多搜索,但似乎找不到答案。 请注意,跑步者抱怨找不到的课程是我要测试/运行的课程。 basicTest.scala看起来像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Testing specific imports
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{ShouldMatchers, WordSpecLike, BeforeAndAfterAll}
import akka.testkit.{TestKit, DefaultTimeout, ImplicitSender}
// Actor specific imports
import akka.actor.{ActorRef, Actor, ActorSystem, Props}
// Misc. needed imports
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory


// In order to run tests in this module you need to use JUnitRunner (as per scalatest.org)
@RunWith(classOf[JUnitRunner])
class BasicActorSpec extends TestKit(ActorSystem("BasicActorSpec", ConfigFactory.parseString(BasicActorSpec.config)))
  with DefaultTimeout with ImplicitSender with WordSpecLike with ShouldMatchers with BeforeAndAfterAll {

  import BasicActorSpec._

  val echoRef = system.actorOf(Props[EchoActor])
  val forwardRef = system.actorOf(Props[ForwardActor])

  override def afterAll {
    shutdown(system)
  }


  /**
   * The actual tests...
   */

 "An EchoActor" should {
   "Respond with the same message it receives" in {
      within(500 millis) {
        echoRef !"test"
        expectMsg("test")
      }
    }
  }

 "A Forwarding Actor" should {
   "Forward a message it receives" in {
      within(500 millis) {
        forwardRef !"test"
        expectMsg("test")
      }
    }
  }

}

/**
 * Companion object of test class
 */

object BasicActorSpec {

  val config =
   """
      |akka {
      | loglevel ="
Warning"
      |}
   "
"".stripMargin

  /**
   * Classes of Actors used in testing
   */

  class EchoActor extends Actor {
    def receive = {
      case msg => sender ! msg
    }
  }

  class ForwardActor(next: ActorRef) extends Actor {
    def receive = {
      case msg => next ! msg
    }
  }

}

对于我为什么收到此错误的任何帮助,将不胜感激。


运行构建项目-当我尝试解决另一个问题时,它帮助我解决了当我清除Cache IDEA :)时可能发生的问题。


这就是我解决相同异常的方法:

1
--> Right click on your project folder in IDE:

enter image description here

1
2
3
--> Click Add Framework Support
--> Then Check Scala
--> Click OK


您需要设置Scala SDK。
1.)通常,intelliJ会在编辑器的右上角显示一条消息来询问您
2)您可以按照https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html上的说明自己进行操作


我在使用加特林时遇到了这个问题

Ive replaced gatling-classes to test-classes

我通过在文件->项目结构->模块->模块名称->路径->测试输出路径上将gatling-classes替换为test-classes来修复它


就我而言,我在模块中缺少Scala方面。

https://blog.jetbrains.com/scala/2010/09/02/project-configuration-explained/

正确配置模块后,我摆脱了错误。


最近,当我尝试使用IntelliJ IDEA 2018(社区版)在继承的Scala项目中运行测试时,发生了此问题。以下步骤可帮助我解决此问题:

  • 文件项目结构{选择特定的模块}"路径"选项卡选择"使用模块编译输出路径",修改"测试输出路径"以指向test-classes文件夹。例如:

    Output path: /home/rustam/IdeaProjects/{project}/{module}/target/scala-2.12/classes
    Test output path: /home/rustam/IdeaProjects/{project}/{module}/target/scala-2.12/test-classes

  • IntelliJ不喜欢在一个文件中定义多个Scala类,因此请确保测试类的名称与测试文件相同,并嵌套其他帮助程序类,以后可以根据需要进行重构。

  • 如果使用IntelliJ运行scalatest,请确保类路径正确。例如:

    1
     /dummyApp

    您的build.sbt应该看起来像name :="dummyApp"。如果将其命名为name :="dummy App",则会出现错误。


    当测试类不属于任何包时,我遇到了错误。


    就我而言,我在Preferences -> Build, Execution, Deployment -> sbt

    启用Use sbt shell for build and import复选框

    测试不在预期的目录src/test中,而在src/it中(集成测试)。