如何使用Scalamock模拟Hadoop FileStatus类?

时间:2019-05-21 15:44:27

标签: hadoop scalatest scalamock

我正在尝试从Hadoop通用API Java库中模拟FileStatus class。导致以下编译器错误:

Error:(34, 24) double definition:
override def compareTo(o: Any): Int at line 34 and
override def compareTo(x$1: T): Int at line 34
have same type after erasure: (o: Object)Int
    val mockFile = mock[FileStatus]
Error:(34, 24) name clash between defined and inherited member:
def compareTo(x$1: T): Int in trait Comparable and
override def compareTo(o: Any): Int at line 34
have same type after erasure: (x$1: Object)Int
    val mockFile = mock[FileStatus]

问题可能出在这个类扩展Comparable而没有指定任何类型参数的事实。来自its source code的相关Java代码段可以在此处查看:

public class FileStatus implements Writable, Comparable {
  // <snipped>
  @Override
  public int compareTo(Object o) {
    FileStatus other = (FileStatus)o;
    return this.getPath().compareTo(other.getPath());
  }
 // <snipped>
}

要重现编译器错误,只需在org.apache.hadoop:hadoop-common:2.6.5上添加一个依赖项,然后创建以下Scalatest方法:

test("my failing test") {
  // your imports
  import org.apache.hadoop.fs.FileStatus
  // code that fails
  val mockFile = mock[FileStatus]
}

是否有任何方法可以解决此问题,以允许通过Scalamock模拟此类?作为参考,它与Scalamock 3.6.0,Scala 2.11和Oracle JDK 1.8.0_181一起使用。

0 个答案:

没有答案