gpt4 book ai didi

java - Guice模块测试: require binding of annotated strings

转载 作者:太空宇宙 更新时间:2023-11-04 10:25:23 26 4
gpt4 key购买 nike

我正在尝试为我在多个项目中使用的库存储库中的常见 guice 模块编写一些测试。某些模块具有注入(inject)项目配置的注释字符串注入(inject)。例如,我有一个 cassandra 模块,需要主机、端口和其他配置的字符串绑定(bind):

class CassandraModule : AbstractModule() {
companion object {
const val CASSANDRA_HOST = "cassandra.host"
const val CASSANDRA_PORT = "cassandra.port"
...

const val MY_CASSANDRA_READ = "casandra.db.read"
const val MY_CASSANDRA_WRITE = "casandra.db.write"
}

override fun configure() {
}

@Provides
@Singleton
@Named(MY_CASSANDRA_READ)
fun provideCassandraReadCluster(
@Named(CASSANDRA_HOST) host: String,
@Named(CASSANDRA_PORT) port: Int,
@Named(CASSANDRA_DATACENTER) dc: String
): Cluster {
val queryOptions = QueryOptions()
queryOptions.consistencyLevel = ConsistencyLevel.LOCAL_ONE

val cluster = Cluster.builder()
.addContactPoint(host)
.withPort(port)
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy(dc))
.withQueryOptions(queryOptions)
.build()
return cluster
}

@Provides
@Singleton
@Named(MY_CASSANDRA_READ)
fun provideCassandraReadSession(
@Named(MY_CASSANDRA_READ) cluster: Cluster,
@Named(CASSANDRA_KEYSPACE) keyspace: String
): Session {
return cluster.connect(keyspace)
}
...
}

所有这些配置字符串绑定(bind)应通过使用此库模块作为依赖项的 Property 绑定(bind)来自其他项目。如何模拟这些带注释的字符串属性?

最佳答案

你为什么要 mock 他们?您可以在测试中注册一个提供这些值的附加模块。

关于java - Guice模块测试: require binding of annotated strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50590495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com