Я пишу юнит-тесты в Scala, используя MockitoSugar . Я использую org.mockito.Mockito.when
и org.mockito.Matchers.anyString
. Тестируемый метод показан ниже.
def getDataSourceToDataFrame(database: scala.Predef.String, tableName: scala.Predef.String): DataFrame = {
glueContext.getCatalogSource(
database = database,
tableName = tableName,
transformationContext = database + "." + tableName + ".read")
.getDynamicFrame()
.toDF()
}
Вот как выглядит мой юнит-тест:
import com.amazon.rrsetlglue.glue.wrappers.WrappedGlueContext
import com.amazonaws.services.glue.{DynamicFrame, GlueContext, DataSource}
import org.scalatest.{BeforeAndAfter, FunSuite, Matchers}
import org.apache.spark.sql.DataFrame
import org.mockito.Mockito.when
import org.mockito.Matchers.anyString
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
// Use Mockito because ScalaMock does not work with Glue
@RunWith(classOf[JUnitRunner])
class WrappedGlueContextTest extends FunSuite with
org.scalatest.mockito.MockitoSugar with BeforeAndAfter {
var mockGlueContext : GlueContext = _
var wrappedGlueContext: WrappedGlueContext = _
before {
mockGlueContext = mock[GlueContext]
wrappedGlueContext = new WrappedGlueContext(mockGlueContext)
}
test("Test get dataFrame from glue data catalog source") {
val mockedDataSource: DataSource= mock[DataSource]
val mockDynamicFrame: DynamicFrame = mock[DynamicFrame]
val mockDataFrame: DataFrame = mock[DataFrame]
assert(wrappedGlueContext.glueContext === mockGlueContext)
when(mockGlueContext.getCatalogSource(anyString, anyString, anyString)).thenReturn(mockedDataSource)
}
}
Последний when()
вызов вызывает ошибку ниже:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 3 recorded:
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
Определение для getCatalogSource()
показано ниже
def getCatalogSource(database: String, tableName: String, redshiftTmpDir: String = "",
transformationContext: String = "", pushDownPredicate: String = "",
additionalOptions: JsonOptions = JsonOptions.empty, catalogId: String = null): DataSource = {
Почему это говорит 0 matchers expected
, когда я вызываю метод getCatalogSource()
на макете?