使用PowerMockito模拟最终课程错误

时间:2020-05-16 06:04:32

标签: scala mockito amazon-dynamodb powermock powermockito

我正在编写测试用例以测试DynamodbMapper功能,如下所示:

  def batchSaveInDDB[T](itemList: List[T]): List[FailedBatch] = AWSSession.dynamoDBMapperDaxEnabled.batchSave(itemList)

这是映射器的创建方式:

 object DynamoDBResource extends Logger {

@throws(classOf[AmazonDynamoDBException])
def getDynamoDBMapperDAXEnabled(region: String): DynamoDBMapper = {

new DynamoDBMapper(getDaxClient(region,
  "*****************"))
 }

  def getAWSCredentialsProvider(): AWSCredentialsProvider = {
   new EC2ContainerCredentialsProviderWrapper()
   }

def getDaxClient(region: String, clusterEndpoint: String): AmazonDynamoDB = {
  AmazonDaxClientBuilder
  .standard()
  .withRegion(region)
  .withCredentials(getAWSCredentialsProvider())
  .withEndpointConfiguration(clusterEndpoint)
  .build()
 }}

测试用例:

  @RunWith(classOf[PowerMockRunner])
 @PrepareForTest(Array(classOf[AmazonDaxClientBuilder]))
 class MockDynamodbDax extends FlatSpec with Matchers{

 def init(): Unit = {

  val amazonDaxClientBuilder: AmazonDaxClientBuilder = 
  PowerMockito.mock(classOf[AmazonDaxClientBuilder])
PowerMockito.mockStatic(classOf[AmazonDaxClientBuilder])
when(AmazonDaxClientBuilder.standard()).thenReturn(amazonDaxClientBuilder)



when(amazonDaxClientBuilder.withCredentials(any(classOf[AWSCredentialsProvider])))
.thenReturn(amazonDaxClientBuilder)
when(amazonDaxClientBuilder.withEndpointConfiguration(anyString()))
.thenReturn(amazonDaxClientBuilder) }

"DDBOperations" should "throw NullPointerException for null data save" in {

  init()
  the[RuntimeException] thrownBy {

  DDBOperations.batchSaveInDDB(null)
} should have message null

}

 "DDBOperations" should "not save duplicate keys and return as FailedBatch" in {
 init()

val employeeDataList = new ArrayList[AnytimePayEmployeeJobData]
employeeDataList.add(getEmployeeJobData(TEST_EMP_ID_1, EFF_END_DATE_1))
employeeDataList.add(getEmployeeJobData(TEST_EMP_ID_1, EFF_END_DATE_2))
val upsertsFailedBatches: List[FailedBatch] = DDBOperations.batchSaveInDDB(employeeDataList)
assert(upsertsFailedBatches.size() == FAILED_RECORDS_COUNT)}}

我遇到了以下例外情况:

应该为空数据保存抛出NullPointerException *失败* (17毫秒) [scalatest] java.lang.IllegalArgumentException:无法子类化最终类类 com.amazon.dax.client.dynamodbv2.AmazonDaxClientBuilder

0 个答案:

没有答案