Mockito 无法模拟 Hadoop Mapper 上下文
Mockito cannot mock Hadoop Mapper Context
所以我正在尝试使用 Mockito 为我的 Hadoop 映射函数创建一个单元测试。我已经正确创建了 Mapper class:
class XmlMapper extends Mapper[LongWritable, Text, Text, LongWritable] {
override def map(key: LongWritable, value: Text, context: Mapper[LongWritable, Text, Text, LongWritable]#Context): Unit = {
//does stuff
}
}
然后我有以下测试:
import org.apache.hadoop.io.{LongWritable, Text}
import org.scalatest.FlatSpec
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito.verify
import org.mockito.Mockito.times
class XmlMapperTest extends FlatSpec with MockitoSugar {
"XmlMapper" should "output" in {
val mapper = new XmlMapper
val context = mock[mapper.Context]
//so far does nothing yet
}
}
但是我收到以下错误:
- should output *** FAILED ***
[info] org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class org.apache.hadoop.mapreduce.Mapper$Context.
[info]
[info] Mockito can only mock non-private & non-final classes.
[info] If you're not sure why you're getting this error, please report to the mailing list.
这没有意义,因为 Mapper.Context 是一个 public 抽象 class。
这里是总堆栈跟踪:
[info] Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection
[info] at org.scalatest.mockito.MockitoSugar.mock(MockitoSugar.scala:73)
[info] at org.scalatest.mockito.MockitoSugar.mock$(MockitoSugar.scala:72)
[info] at XmlMapperTest.mock(XmlMapperTest.scala:7)
[info] at XmlMapperTest.$anonfun$new(XmlMapperTest.scala:11)
[info] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[info] at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
[info] at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
[info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info] at org.scalatest.Transformer.apply(Transformer.scala:22)
[info] at org.scalatest.Transformer.apply(Transformer.scala:20)
看来您使用的是旧版本的 mockito-core,鉴于您使用的是 Scala,我强烈建议您使用最新版本的 mockito-scala
此外,作为一种良好做法,您应该尽可能避免嘲笑您不拥有的 classes。
更好的方法是将与第 3 方的交互包装在 class(或一组 classes)中并进行集成测试以证明它们按预期工作。
在系统的其余部分,您注入这些 classes,因此,为了测试,您模拟说 classes,这允许您控制(并简化)暴露的接口并在第三方库的未来版本更改其 API.
的情况下最大限度地减少中断
有关详细信息,请查看 Mockito 博客上的 and this post
所以我正在尝试使用 Mockito 为我的 Hadoop 映射函数创建一个单元测试。我已经正确创建了 Mapper class:
class XmlMapper extends Mapper[LongWritable, Text, Text, LongWritable] {
override def map(key: LongWritable, value: Text, context: Mapper[LongWritable, Text, Text, LongWritable]#Context): Unit = {
//does stuff
}
}
然后我有以下测试:
import org.apache.hadoop.io.{LongWritable, Text}
import org.scalatest.FlatSpec
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito.verify
import org.mockito.Mockito.times
class XmlMapperTest extends FlatSpec with MockitoSugar {
"XmlMapper" should "output" in {
val mapper = new XmlMapper
val context = mock[mapper.Context]
//so far does nothing yet
}
}
但是我收到以下错误:
- should output *** FAILED ***
[info] org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class org.apache.hadoop.mapreduce.Mapper$Context.
[info]
[info] Mockito can only mock non-private & non-final classes.
[info] If you're not sure why you're getting this error, please report to the mailing list.
这没有意义,因为 Mapper.Context 是一个 public 抽象 class。
这里是总堆栈跟踪:
[info] Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection
[info] at org.scalatest.mockito.MockitoSugar.mock(MockitoSugar.scala:73)
[info] at org.scalatest.mockito.MockitoSugar.mock$(MockitoSugar.scala:72)
[info] at XmlMapperTest.mock(XmlMapperTest.scala:7)
[info] at XmlMapperTest.$anonfun$new(XmlMapperTest.scala:11)
[info] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[info] at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
[info] at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
[info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info] at org.scalatest.Transformer.apply(Transformer.scala:22)
[info] at org.scalatest.Transformer.apply(Transformer.scala:20)
看来您使用的是旧版本的 mockito-core,鉴于您使用的是 Scala,我强烈建议您使用最新版本的 mockito-scala
此外,作为一种良好做法,您应该尽可能避免嘲笑您不拥有的 classes。 更好的方法是将与第 3 方的交互包装在 class(或一组 classes)中并进行集成测试以证明它们按预期工作。
在系统的其余部分,您注入这些 classes,因此,为了测试,您模拟说 classes,这允许您控制(并简化)暴露的接口并在第三方库的未来版本更改其 API.
的情况下最大限度地减少中断有关详细信息,请查看 Mockito 博客上的