我试图在我的库仓库中为我的常见guice模块编写一些测试,我在几个项目中使用它们。一些模块具有注释字符串注入,注入项目配置。例如,我有一个cassandra模块,需要对主机,端口和其他配置进行String绑定:
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)
}
...
}
所有这些配置字符串绑定应该来自其他项目,通过使用此库模块作为依赖项的Property
绑定。如何模拟这些带注释的字符串属性?
答案 0 :(得分:0)
你为什么要嘲笑他们?您可以在测试中注册一个提供这些值的附加模块。