scala 在 IntelliJ 中运行 ScalaTest 时“无法加载套件类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18838012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Unable to load a Suite class' while running ScalaTest in IntelliJ
提问by MCP
I'm having some issues today with running a simple TestKit test in Intellij. The tests are for Scala code (I have the Scala plug-in for Intellij) and are based on Ray Roestenburg's example.
我今天在 Intellij 中运行一个简单的 TestKit 测试时遇到了一些问题。这些测试是针对 Scala 代码的(我有 Intellij 的 Scala 插件)并且基于Ray Roestenburg 的示例。
The Intellij project was created using a "Maven Module" which I then added all the dependencies to and created my project. The tests are located in the following place:
Intellij 项目是使用“Maven 模块”创建的,然后我将所有依赖项添加到其中并创建了我的项目。测试位于以下位置:
ActorBlast/src/test/scala/basicTest.scala
ActorBlast/src/test/scala/basicTest.scala
I'm basically "right-clicking" on the test and selecting "Run". What I get is the following error:
我基本上是“右键单击”测试并选择“运行”。我得到的是以下错误:
"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
"C:\Program Files\Java\jdk1.7.0_25\bin\java" -Didea.launcher.port=7540... 测试于下午 2:29 开始...无法加载 Suite 类。这可能是由于您的运行路径中的错误。
缺少类: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) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) 在 org.scalatest.tools.Runner$$anonfun$35.apply(Runner.scala:2393) 在 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。收藏。TraversableLike$class.filter(TraversableLike.scala:263) at scala.collection.AbstractTraversable.filter(Traversable.scala:105) at org.scalatest.tools.Runner$.doRunRunRunDaDoRun(Runner.scala:2391) at org.scalatest. tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1006) 在 org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1005) 在 org.scalatest$tools.Runner .withClassLoaderAndDispatchReporter(Runner.scala:2659) 在 org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1005) 在 org.scalatest.tools.Runner$.run(Runner.scala:845) 在 org.scalatest.run(Runner.scala:845) tools.Runner.run(Runner.scala) 在 org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:144) 在 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)execution.application.AppMain.main(AppMain.java:120)execution.application.AppMain.main(AppMain.java:120)
进程完成,退出代码 0
I can't figure out what this means. I've done a lot of searching but can't seem to find an answer. Note that the class the runner is complaining about not finding is the class I'm trying to test/run. The basicTest.scala looks like this:
我无法弄清楚这是什么意思。我已经做了很多搜索,但似乎无法找到答案。请注意,跑步者抱怨找不到的课程是我正在尝试测试/运行的课程。basicTest.scala 看起来像这样:
// 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
}
}
}
Any help as to why I am getting this error would be GREATLY appreciated.
任何有关为什么我收到此错误的帮助将不胜感激。
回答by AlexPes
Run build the project - It helped me to resolve that issue that could have happened to me when I cleared Cache IDEA :) while trying to tackle another issue
运行构建项目 - 它帮助我解决了在我尝试解决另一个问题时清除缓存 IDEA 时可能发生的问题:)
回答by yantaq
回答by Alex Elkin
回答by AMS
You need to set up the Scala SDK. 1.) Usually, intelliJ will ask you by showing a message on right hand corner of your editor 2.) You can do it by yourself as mentioned on the https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html
您需要设置 Scala SDK。1.) 通常,intelliJ 会通过在编辑器的右上角显示一条消息来询问您 2.) 您可以按照https://www.jetbrains.com/help/idea/discover-intellij中所述自行完成-idea-for-scala.html
回答by fglez
In my case, I was missing the Scala facet in my module.
就我而言,我的模块中缺少 Scala 方面。
https://blog.jetbrains.com/scala/2010/09/02/project-configuration-explained/
https://blog.jetbrains.com/scala/2010/09/02/project-configuration-explained/
I got rid of the error once I configured module properly.
一旦我正确配置了模块,我就摆脱了错误。
回答by Rustam Aliyev
This issue happened to me recently when I was trying to run tests in an inherited Scala project using IntelliJ IDEA 2018 (Community Edition). Below steps helped me to fix it:
最近当我尝试使用IntelliJ IDEA 2018 (Community Edition)在继承的 Scala 项目中运行测试时,这个问题发生在我身上。以下步骤帮助我修复它:
- File → Project Structure → {Choose specific module} → "Paths" tab → Select "Use module compile output path" → Modify "Test output path" to point to test-classesfolder. For example:
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 does not like multiple Scala classes defined in one file, so make sure the test class is named the same as the test file and nest other helper classes, which you can refactor later as necessary.
- 文件→项目结构→{选择特定模块}→“路径”选项卡→选择“使用模块编译输出路径”→修改“测试输出路径”以指向test-classes文件夹。例如:
输出路径:/home /rustam/IdeaProjects/{project} /{module}/target/scala-2.12/classes
测试输出路径:/home/rustam/IdeaProjects/{project}/{module}/target/scala-2.12/测试班 - IntelliJ 不喜欢在一个文件中定义多个 Scala 类,因此请确保测试类的名称与测试文件相同,并嵌套其他辅助类,您可以稍后根据需要对其进行重构。
回答by Ray Goodwin
If you are using IntelliJ to run scalatest make sure the class paths are correct. For example:
如果您使用 IntelliJ 运行 Scalatest,请确保类路径正确。例如:
/dummyApp
your build.sbtshould look like, name := "dummyApp". If you name it name := "dummy App"you will get errors.
你build.sbt应该看起来像,name := "dummyApp"。如果你命名它,name := "dummy App"你会得到错误。
回答by m3th0dman
In my case I had in Preferences -> Build, Execution, Deployment -> sbt
就我而言,我曾在 Preferences -> Build, Execution, Deployment -> sbt
Use sbt shell for build and importcheckbox enabled
Use sbt shell for build and import复选框已启用
And the test was not in the expected directory src/testbut in src/it(integrated test).
并且测试不在预期的目录中,src/test而是在src/it(集成测试)中。
回答by AdrianS
I encountered the error when the test class was not part of any package.
当测试类不属于任何包时,我遇到了错误。
回答by Raj
My project already had the setup as mentioned by @Rustam Aliyev. Still was getting the same exception. Rebuilding the project did not help either. Quite weird ; but Restarting the IDE helped to solve the issue
我的项目已经有了@Rustam Aliyev 提到的设置。仍然得到同样的例外。重建项目也无济于事。很奇怪;但重新启动 IDE 有助于解决问题


