mockito 模拟尝试初始化字段

v8wbuo2f  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(228)

我有一个类,我试图模拟并调用它的方法-

@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)
  }
}

我在另一个类中使用这个方法-

@Service
open class SizingService(private val cloudStorageService: CloudStorageService) {
 fun methodName() {
   ...
   val fileSize = cloudStorageService.getSizeOfFirstMatchedObject("bucketName", "filename", "prefix") // trying to mock this call
   ...
 }
}

我的测试文件:

@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)
  }
}

s3 Client在我的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()
  }
}

但是当我运行测试时,我得到一个失败消息,说s3 Client未初始化。为什么它甚至试图在CloudStorageService中初始化s3 Client,而它是一个mock?

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)

我如何正确地嘲笑这个方法?

70gysomp

70gysomp1#

我做了一个方法,我正在模拟open,模拟工作。但我不知道为什么mockito试图初始化,如果方法没有打开。错误消息也没有帮助。
有人能解释一下mockito实际上在做什么,以及方法上的open如何允许mock发生吗?

// 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)
  }

相关问题