[英]Mock trying to initialize fields
I have a class that I am trying to mock and call a method on it -我有一个类,我试图模拟并在其上调用一个方法-
@Component open class CloudStorageService {
@Autowired lateinit var s3Client: AmazonS3
fun getSizeOfFirstMatchedObject(bucketName: String, directory: String, prefix: String): Long {
val request = ListObjectsRequest().withBucketName(bucketName).withPrefix("$directory/$prefix")
val listObjects = s3Client.listObjects(request)
val objectSummary = listObjects.objectSummaries.first()
val size = objectSummary.size
return size / (1024 * 1024)
}
}
I am using this method in another class as -我在另一个类中使用这种方法 -
@Service
open class SizingService(private val cloudStorageService: CloudStorageService) {
fun methodName() {
...
val fileSize = cloudStorageService.getSizeOfFirstMatchedObject("bucketName", "filename", "prefix") // trying to mock this call
...
}
}
My Test File:我的测试文件:
@ActiveProfiles("test")
@RunWith(SpringRunner::class)
@SpringBootTest(classes = [TestAppConfig::class])
class SizingServiceTest {
@Mock private lateinit var cloudStorageService: CloudStorageService
@Before
fun setUp() {
MockitoAnnotations.openMocks(this)
sizingService = SizingService(cloudStorageService)
}
@Test
fun shouldReturnDropletSizeForChainBusinessesCorrectly() {
`when`(cloudStorageService.getSizeOfFirstMatchedObject(eq("b1")!!, eq("filename"), eq(prefix))).thenReturn(200)
}
}
The s3Client is initialized as a bean in my AppConfig - s3Client 在我的 AppConfig 中被初始化为 bean -
@Configuration
@EnableTransactionManagement
@EnableScheduling
open class AppConfig {
@Value("\${S3_REGION}") lateinit var awsRegion: String
@Value("\${S3_ACCESS_KEY}") lateinit var accessKey: String
@Value("\${S3_SECRET_KEY}") lateinit var secretKey: String
@Bean(name = ["s3Client"])
open fun s3Client(): AmazonS3 {
return AmazonS3ClientBuilder.standard().withRegion(awsRegion).withCredentials(AWSStaticCredentialsProvider(BasicAWSCredentials(accessKey, secretKey))).build()
}
}
But when I run the test, I get a failure saying s3Client is uninitialized.但是当我运行测试时,我得到一个失败,说 s3Client 未初始化。 Why is it even trying to initialize the s3Client in
CloudStorageService
when it is a mock ?为什么当它是一个 mock 时,它甚至试图在
CloudStorageService
中初始化 s3Client ?
lateinit property s3Client has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property s3Client has not been initialized
at com.service.CloudStorageService.getS3Client(CloudStorageService.kt:33)
at com.service.CloudStorageService.getSizeOfFirstMatchedObject(CloudStorageService.kt:113)
at com.service.SizingServiceTest.shouldReturnDropletSizeForChainBusinessesCorrectly(SizingServiceTest.kt:219)
How do I properly mock this method ?如何正确模拟此方法?
I made the method that I am mocking open
and the mocking worked.我使我正在模拟的方法
open
并且模拟工作。 But I have no idea why mockito was trying to initialize if the method is not open.但我不知道为什么如果方法未打开,mockito 会尝试初始化。 The error message also did not help.
错误消息也没有帮助。
Can someone explain what mockito is actually doing and how open
on a method allows the mock to happen ?有人可以解释 mockito 实际在做什么以及方法的
open
性如何允许模拟发生吗?
// Only Change I did was adding `open`.
open fun getSizeOfFirstMatchedObject(bucketName: String, directory: String, prefix: String): Long {
val request = ListObjectsRequest().withBucketName(bucketName).withPrefix("$directory/$prefix")
val listObjects = s3Client.listObjects(request)
val objectSummary = listObjects.objectSummaries.first()
val size = objectSummary.size
return size / (1024 * 1024)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.