使用Guice DI的Cucumber测试无法识别@Named注解求助
问题:Cucumber-Guice 7.11集成测试中@Named依赖注入失败
我正尝试使用Cucumber-Guice 7.11编写集成测试,遇到@Named注解的依赖注入失败问题,以下是相关信息:
目录结构
src/main/kotlin/ \_ com.mypackage.tests \_ api/ApiGatewayHttpClient.kt \_ cucumber \_ api \_ ApiInfraTestsModule.kt \_ CucumberInjectorSource.kt \_ InfraTestsModule.kt \_ steps \_ EdgeApiSteps.kt
相关代码
ApiInfraTestsModule.kt
class ApiInfraTestsModule : AbstractModule() { private val ROLE_ARN_FORMATTER = "arn:aws:iam::%s:role/%s" @Provides @Singleton fun providesCloudFormationExportsExtractor(): BiFunction<String, String, String> { return BiFunction { region: String, exportName: String -> // some CloudFormation logic, return export value } } @Provides @Singleton @Named(EDGE_API_ENDPOINT) fun providesEdgeApiEndpoint( cloudFormationExportsExtractor: BiFunction<String, String, String> ): String { val region = Environment.region return cloudFormationExportsExtractor.apply( region, EDGE_API_ENDPOINT_EXPORT_NAME ) } @Provides @Singleton @Named(EDGE_API_UNAUTHORIZED_CREDENTIALS_PROVIDER) fun providesApiUnauthorizedCredentialsProvider( // this param is supposed to be injected via `InfraTestsModule.kt` awsCredentialsProvider: BiFunction<String, String, AwsCredentialsProvider> ): AwsCredentialsProvider { val region = Environment.region val roleArn = String.format( ROLE_ARN_FORMATTER, Environment.plantFloorAccountId, Environment.unauthorizedEdgeProvisioningRoleName ) return awsCredentialsProvider.apply(region, roleArn) } @Provides @Singleton @Named(EDGE_API_UNAUTHORIZED_CLIENT) fun providesEdgeApiUnAuthorizedClient( @Named(EDGE_API_UNAUTHORIZED_CREDENTIALS_PROVIDER) credentialsProvider: AwsCredentialsProvider ): ApiGatewayHttpClient { return ApiGatewayHttpClient( Region.of(Environment.region),credentialsProvider ) } }
CucumberInjectorSource.kt
class CucumberInjectorSource : InjectorSource { @ScenarioScoped override fun getInjector(): Injector { return Guice.createInjector( Stage.PRODUCTION, CucumberModules.createScenarioModule(), InfraTestsModule(), ApiInfraTestsModule() ) } }
EdgeApiSteps.kt
class EdgeApiSteps @Inject constructor( private val testContext: TestContext, @Named(EDGE_API_ENDPOINT) private val endpoint: String, @Named(EDGE_API_UNAUTHORIZED_CLIENT) private val unauthorizedApiClient: ApiGatewayHttpClient ) { @When("Provisioning API is called with unauthorized client") fun executeUnauthorizedApiCall() { val response = unauthorizedApiClient.get(endpoint) testContext.apiResponseStatus = response.httpResponse().statusCode() } }
错误信息
运行测试时报错:
Failed scenarios: file:////features/security/api/EdgeApi.feature:2 # Provisioning API call with unauthorized principal should fail 1 Scenarios (1 failed) 2 Steps (1 failed, 1 skipped) 0m0.663s com.google.inject.ConfigurationException: Guice configuration errors: 1) [Guice/MissingImplementation]: No implementation for ApiGatewayHttpClient annotated with @Named(value="EdgeApiUnauthorizedClient") was bound. Requested by: 1 : EdgeApiSteps.<init>(EdgeApiSteps.kt:11) \_ for 3rd parameter while locating EdgeApiSteps 2) [Guice/MissingImplementation]: No implementation for String annotated with @Named(value="EdgeApi") was bound. Requested by: 1 : EdgeApiSteps.<init>(EdgeApiSteps.kt:11) \_ for 2nd parameter while locating EdgeApiSteps 2 errors
排查情况
- 查阅过Guice相关示例,确认Guice支持Kotlin的@Named注入,但问题未解决
- 怀疑Cucumber无法识别Guice模块,即使简化模块仅提供单个@Named字符串参数,仍出现相同的「No implementation bound」错误
- 尝试过构造函数注入、方法注入、字段注入三种方式,结果均触发上述错误
内容的提问来源于stack exchange,提问作者Sleepinator
相关产品推荐
相关产品推荐

