I want to test a method we have that is formatted something like this:
def extractTable( spark: SparkSession, /* unrelated other parameters */ ): DataFrame = {
// Code before that I want to test
val df = spark.read
.format("jdbc")
.option("url", "URL")
.option("driver", "<Driver>")
.option("fetchsize", "1000")
.option("dbtable", "select * from whatever")
.load()
// Code after that I want to test
}
And I am trying to make stubs of the spark object, and the DataFrameReader objects that the read
and option
methods return:
val sparkStub = stub[ SparkSession ]
val dataFrameReaderStub = stub[ DataFrameReader ]
( dataFrameReaderStub.format _).when(*).returning( dataFrameReaderStub ) // Works
( dataFrameReaderStub.option _).when(*, *).returning( dataFrameReaderStub ) // Error
( dataFrameReaderStub.load _).when(*).returning( ??? ) // Return a dataframe // Error
( sparkStub.read _).when().returning( dataFrameReaderStub )
But I am getting an error on dataFrameReaderStub.option
and dataFrameReaderStub.load
that says "Cannot resolve symbol option" and "Cannot resolve symbol load". But these methods definitely exist on the object that spark.read
returns.
How can I resolve this error, or is there a better way to mock/test the code I have?