首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用scalatest编写的具有单元测试的纯java项目具有编译器兼容性问题

使用scalatest编写的具有单元测试的纯java项目具有编译器兼容性问题
EN

Stack Overflow用户
提问于 2022-01-15 18:05:26
回答 1查看 117关注 0票数 0

我们有一个简单的java项目,其中包含用scalatest编写的单元测试:

编译范围中的scala库( false)

  • scala-compiler scalatest

  • scala-library,autoScalaLibrary :=
      • ) (autoScalaLibrary:=添加到测试范围中,以编译用scala编写的单元测试,使用添加到单元测试的测试范围)。

从intellij和命令行sbt控制台执行单元测试时出现以下错误:

代码语言:javascript
复制
scalac: error while loading package, class file '/Users/loc/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.8/scala-library-2.13.8.jar(scala/reflect/package.class)' is broken
(class java.lang.RuntimeException/error reading Scala signature of package.class: Scala signature package has wrong version
 expected: 5.0
 found: 5.2 in package.class)

以下是简化的build.sbt文件:-

代码语言:javascript
复制
name := "pure-java-project-with-scala-tests"

//this is added for packaging
enablePlugins(PackPlugin)

//this will not add scala library to compile classpath, as our project is a pure java project
//this will also not add scala library when "sbt pack" is triggered
autoScalaLibrary := false
//this will disable cross-building, as our project is a pure java project
crossPaths := false

//this will add scalatest to test classpath, as our project unit tests will be written using scalatest
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % Test
//this will add scala library classes to test classpath, as our project unit tests will be written using scalatest
libraryDependencies += "org.scala-lang" % "scala-library" % "2.13.8" % Test

//our assumption is scala-compiler, it is required to compile scala unit tests
//but it is giving below error, when I try to execute unit test from intellij as well as sbt
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.13.8" % Test

简化的项目可以在https://github.com/moglideveloper/pure-java-project-with-scala-tests上找到

sbt gitter链接:- https://gitter.im/sbt/sbt?at=61e30f06f5a3947800088f3d

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-15 18:52:37

由于您没有为项目定义Scala版本,所以当您执行%%以包含ScalaTest时,它使用了sbt的默认版本,即2.12,从而导致二进制不兼容。

我认为最好的解决方案是使用定义项目的适当版本,并删除它的显式依赖。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70724032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档