单元测试用例模拟postgresql连接和SCALA中的语句

时间:2017-07-04 09:31:19

标签: postgresql scala

我是Scala的新手,需要编写一个测试用例来模拟Postgresql连接和Statements.However无法这样做并得到错误。任何人都可以帮助我。下面是我写的代码 在此先感谢!!

import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.Column`
import org.slf4j.LoggerFactory
import java.nio.file.Paths
import java.sql.ResultSet
import java.io.InputStream
import java.io.Reader
import java.util
import java.io.File
import java.util.UUID
import java.nio.file.attribute.PosixFilePermission
import com.typesafe.config.ConfigFactory
import org.apache.spark.sql.{DataFrame, SQLContext}
import org.scalatest.{Matchers, WordSpecLike, BeforeAndAfter}
import org.scalactic.{Good, Bad, Many, One}
import scala.collection.JavaConverters._
import spark.jobserver.{SparkJobValid, SparkJobInvalid}
import spark.jobserver.api.{JobEnvironment, SingleProblem}
import org.apache.spark.sql.{Column, Row, DataFrame}
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.easymock.EasyMock.expect
import org.powermock.api._
import org.powermock.core.classloader.annotations.PrepareForTest
import java.io.FileReader
import org.scalamock.scalatest.MockFactory
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.api.mockito.PowerMockito
import org.powermock.api.mockito.PowerMockito._
import org.postgresql.copy.CopyManager
import scala.collection.JavaConversions._
import org.mockito.Matchers.any
import java.sql.Statement

class mockCopyManager(){

def copyIn(command : String , fR:java.io.FileReader) :Unit ={
println("Run Command {}".format(command))
}

}

class AdvisoretlSpec extends WordSpecLike    with Matchers  with     
MockFactory {
val sc = SparkUnitTestContext.hiveContext
import SparkUnitTestContext.defaultSizeInBytes


"Class Advisoretl job" should {

"load data in "{

  val csvMap : Map[String,String] = Map("t1"->"t1.csv","t2"->"t2.csv")
  val testObj = new Advisoretl()
  val mockStatement = mock[Statement]
  val mockConnection=mock[Connection]
  val a:String = "TRUNCATE TABLE t1"
  val b:String = "TRUNCATE TABLE t2"
  PowerMockito.mockStatic(classOf[DriverManager])
  val mockCopyManager=mock[CopyManager]
  PowerMockito.when(DriverManager.getConnection(any[String]), Nil: _*).thenReturn(mockConnection)
  (mockConnection.createStatement _).when().returns(mockStatement)
  (mockStatement.executeUpdate _).when(a).returns(1)
  (mockStatement.executeUpdate _).when("TRUNCATE TABLE t2").returns(1)
  (mockCopyManager.copyIn _).when(*).returns(1)*/
  val fnResult = testObj.connectionWithPostgres("a", "b", "c", "target/testdata", csvMap)
  fnResult should be ("OK")

}
  }
}'

0 个答案:

没有答案