java.lang.NoClassDefFoundError:用于gradle JUNIT测试的scala / Product $ class

时间:2019-01-30 03:34:33

标签: java scala gradle

我有一个带有build.gradle的scala应用程序(使用gradleVersion 4.4.1)。我正在使用JUnit Framework。我的gradle.build的依赖项和测试任务部分在下面

dependencies {
    compile 'org.scala-lang:scala-library:2.11.12'
    compile 'org.apache.spark:spark-core_2.11:2.0.2'
    compile 'org.apache.spark:spark-sql_2.11:2.0.2'
    compile 'org.apache.spark:spark-hive_2.11:2.0.2'
    compile 'com.typesafe.play:play-json_2.12:2.6.9'
    compile 'com.fasterxml.jackson.module:jackson-module-jsonSchema:2.9.8'
    testCompile 'org.scalatest:scalatest_2.11:3.0.0'
    testCompile 'junit:junit:4.12'
}


test {
    testLogging.showStandardStreams = true
}

当我进行gradle测试时

  

任务:测试失败

com.someOrg.gdpschemautils.JsonToSchemaSpec > initializationError FAILED
    java.lang.NoClassDefFoundError
        Caused by: java.lang.ClassNotFoundException

1个测试完成,1个失败

当我运行gradle test --debug

19:20:21.303 [DEBUG] [TestEventLogger] com.someOrg.gdpschemautils.JsonToSchemaSpec > initializationError FAILED
19:20:21.303 [DEBUG] [TestEventLogger]     java.lang.NoClassDefFoundError: scala/Product$class
19:20:21.303 [DEBUG] [TestEventLogger]         at org.scalatest.time.Units.<init>(Units.scala:33)
19:20:21.304 [DEBUG] [TestEventLogger]         at org.scalatest.time.Days$.<init>(Units.scala:293)
19:20:21.304 [DEBUG] [TestEventLogger]         at org.scalatest.time.Days$.<clinit>(Units.scala)
19:20:21.304 [DEBUG] [TestEventLogger]         at org.scalatest.time.Span$.<init>(Span.scala:584)
19:20:21.304 [DEBUG] [TestEventLogger]         at org.scalatest.time.Span$.<clinit>(Span.scala)
19:20:21.304 [DEBUG] [TestEventLogger]         at org.scalatest.Suite$.<init>(Suite.scala:1399)
19:20:21.304 [DEBUG] [TestEventLogger]         at org.scalatest.Suite$.<clinit>(Suite.scala)

下面是应用程序结构。

.
├── README.md
├── build.gradle
├── gradle.properties
├── settings.gradle
├── gradlew
├── gradlew.bat
├── src
  └──  main
    └──  scala
       └── com
           └── someOrg
               └── gdpschemautils
                    ├── Config
                    ├── DataSourcePathException
                    ├── JsonToSchema
                    ├── ObjectToJsonSchema
                    ├── SchemaConverter
                    ├── SchemaToJson

  └── test
    └── resources
        ├── testJsonSchema.json

    └── scala
       └── com
           └── someOrg
               └── gdpschemautils
                    ├── JsonToSchemaSpec

似乎scala类在运行时不可用于单元测试,我在我的依赖项中也为scala库添加了testCompile,但仍然遇到相同的错误。我的任何单元测试都不会运行,而是JsonToSchemaSpec或其他。

1 个答案:

答案 0 :(得分:0)

当您意外混合使用不同的不兼容版本的scala / spark / scaletest时,通常会发生这种情况。例如查看NoClassDefFoundError: scala/Product$class