我正在尝试为awazon s3编写单元测试用例来上传文件。我使用了mokito。 以下是我在S3中插入文件的代码:
def insertingFilesInS3(path: String, file: File): Boolean = {
try {
s3client.putObject(BUCKET_NAME, path, file)
true
} catch {
case ex: Exception => info(s"File storage failed for $path $file" + ex.printStackTrace()); false
}}
到目前为止,我写过:
val s3: AmazonS3Client = mock[AmazonS3Client]("s3")
val messageDigest = MessageDigest.getInstance("MD5")
val bucket = "bucket"
val keyName = "keyName"
val file: File = mock[File]
val expectedResult: PutObjectResult = mock[PutObjectResult]
val objectmetadata: ObjectMetadata =mock[ObjectMetadata]
"return true when inserting files in s3" in {
when(s3.putObject(bucket, keyName, file).setMetadata(objectmetadata)).thenReturn(expectedResult)
val result = S3Util.insertingFilesInS3(keyName, file)
assert(!result)
}
断言statemnt抛出异常,结果我得到了假。 我得到空指针异常:
java.lang.NullPointerException
at com.amazonaws.services.s3.internal.Mimetypes.getMimetype(Mimetypes.java:160)
at com.amazonaws.services.s3.internal.Mimetypes.getMimetype(Mimetypes.java:201)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1642)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1604)
at s3Utility.S3Util$class.insertingFilesInS3(S3Util.scala:15)
at s3Utility.S3Util$.insertingFilesInS3(S3Util.scala:52)
at com.codesquad.test.S3UtilityTest.S3UtilTest$$anonfun$1.apply(S3UtilTest.scala:28)
at com.codesquad.test.S3UtilityTest.S3UtilTest$$anonfun$1.apply(S3UtilTest.scala:20)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.WordSpecLike$$anon$1.apply(WordSpecLike.scala:1078)
at org.scalatest.TestSuite$class.withFixture(TestSuite.scala:196)
at com.codesquad.test.S3UtilityTest.S3UtilTest.withFixture(S3UtilTest.scala:12)
at org.scalatest.WordSpecLike$class.invokeWithFixture$1(WordSpecLike.scala:1075)
at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
at org.scalatest.WordSpecLike$class.runTest(WordSpecLike.scala:1088)
at com.codesquad.test.S3UtilityTest.S3UtilTest.org$scalatest$BeforeAndAfter$$super$runTest(S3UtilTest.scala:12)
at org.scalatest.BeforeAndAfter$class.runTest(BeforeAndAfter.scala:203)
at com.codesquad.test.S3UtilityTest.S3UtilTest.runTest(S3UtilTest.scala:12)
at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
at scala.collection.immutable.List.foreach(List.scala:392)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:379)
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
at org.scalatest.WordSpecLike$class.runTests(WordSpecLike.scala:1147)
at com.codesquad.test.S3UtilityTest.S3UtilTest.runTests(S3UtilTest.scala:12)
at org.scalatest.Suite$class.run(Suite.scala:1147)
at com.codesquad.test.S3UtilityTest.S3UtilTest.org$scalatest$WordSpecLike$$super$run(S3UtilTest.scala:12)
at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
at org.scalatest.WordSpecLike$class.run(WordSpecLike.scala:1192)
at com.codesquad.test.S3UtilityTest.S3UtilTest.org$scalatest$BeforeAndAfter$$super$run(S3UtilTest.scala:12)
at org.scalatest.BeforeAndAfter$class.run(BeforeAndAfter.scala:258)
at com.codesquad.test.S3UtilityTest.S3UtilTest.run(S3UtilTest.scala:12)
at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1340)
答案 0 :(得分:0)
您正在尝试调用模拟对象的方法,而不说明它应该返回什么。所以首先你要说明这一点。此外,您正在链接对s3对象的方法调用。 setMetadata()没有返回值作为Unit函数,并且要求它返回PutObjectResult类型的 expectedResult 对象,这会给你一个编译错误。相反,请尝试以下方法。
"return true when inserting files in s3" in {
when(s3.putObject(bucket, keyName, file)).thenReturn(expectedResult)
val result = S3Util.insertingFilesInS3(keyName, file)
assert(!result)
}