文章目录
前言
您可以创建 java.util.Map 的实例,还可以指定每个键值条目应使用哪种类型的值。仔细观察,这也是基于我们基本数据类型生成器
一、map自动生成器
def 'generate a map'() {
when: 'defining a map with different fields'
def myMap = Gen.map(
id: Gen.getLong(),
name: Gen.string(10),
age: Gen.integer(0, 120)).iterator().next()
then: 'we should get instances of map'
myMap instanceof Map
and: 'the fields should follow the generators rules'
println(myMap)
myMap.id instanceof Long
myMap.name instanceof String
myMap.age instanceof Integer
}
运行结果:
[id:-4952802953859573409, name:s, age:97]
总结
实际应用中map的使用比较多,且一些算法中也经常用到,因此熟练掌握此方法可以有效的针对性测试。