Junit5 jqwik-任意映射-在映射中生成随机数目的条目

Junit5 jqwik-任意映射-在映射中生成随机数目的条目,junit5,jqwik,Junit5,Jqwik,此代码用于为元素生成单个映射条目。但是我想使用generateInputMapElements和 传递到从ApplyingRule()返回的状态 @属性 //@报告(Reporting.GENERATED) 布尔状态从ApplyingRule(@ForAll(“generateRule”)规则返回, @ForAll(“generateInputMapElements”)可编辑元素){ RangeMatchRule RangeMatchRule=新的RangeMatchRule(); 最终规则if

此代码用于为元素生成单个映射条目。但是我想使用generateInputMapElements和 传递到从ApplyingRule()返回的状态

@属性
//@报告(Reporting.GENERATED)
布尔状态从ApplyingRule(@ForAll(“generateRule”)规则返回,
@ForAll(“generateInputMapElements”)可编辑元素){
RangeMatchRule RangeMatchRule=新的RangeMatchRule();
最终规则if.Status Status=rangeMatchRule.applyRule(规则,元素);
返回RuleIF.getEnums().contains(status.toString());
}
@供给
任意generateInputMapElements(){
任意metricValueArb=任意数.doubles()
.介于(0,50.0)之间;
任意输入maparb=
metricValueArb.map(metricsValue->{
Map inputMap=newhashmap();
inputMap.put(Utils.METRIC\u值,metricsValue);
返回inputMap;
});
返回inputMapArb.map(inputMap->{
List inputMapLst=new ArrayList();
inputMapLst.add(inputMap);
返回inputMapLst;
});
}

假设您想要一个带有单个条目的地图列表(可编辑),我看到两个基本选项

选项1-使用
任意.list()
生成列表,并直接在生成器代码中指定最小和最大大小:

@Provide
Arbitrary<List<Map<String, Object>>> generateInputMapElements() {
    Arbitrary<Double> metricValueArb = Arbitraries.doubles()
                                                  .between(0, 50.0);

    return metricValueArb
        .map(metricsValue -> {
            Map<String, Object> inputMap = new HashMap<>();
            inputMap.put(Utils.METRIC_VALUE, metricsValue);
            return inputMap;
        })
        .list().ofMinSize(1).ofMaxSize(10);
}
@提供
任意generateInputMapElements(){
任意metricValueArb=任意数.doubles()
.介于(0,50.0)之间;
返回度量值arb
.map(度量值->{
Map inputMap=newhashmap();
inputMap.put(Utils.METRIC\u值,metricsValue);
返回inputMap;
})
.list().ofminize(1).ofMaxSize(10);
}
选项2-仅生成单个地图,并对iterable使用标准注释:

@Property
@Report(Reporting.GENERATED)
boolean statusReturnedFromApplyingRule2(
    @ForAll("generateRule") Rule rule,
    @ForAll @Size(min = 1, max = 10) Iterable<@From("generateInputMap") Map<String, Object>> elements
) {
    ...
}

@Provide
Arbitrary<Map<String, Object>> generateInputMap() {
    Arbitrary<Double> metricValueArb = Arbitraries.doubles()
                                                  .between(0, 50.0);

    return metricValueArb
        .map(metricsValue -> {
            Map<String, Object> inputMap = new HashMap<>();
            inputMap.put(Utils.METRIC_VALUE, metricsValue);
            return inputMap;
        });
}
@属性
@报告(Reporting.GENERATED)
从ApplyingRule2返回的布尔状态(
@ForAll(“生成规则”)规则,
@对于所有尺寸(最小值=1,最大值=10)的可更换元件
) {
...
}
@供给
任意generateInputMap(){
任意metricValueArb=任意数.doubles()
.介于(0,50.0)之间;
返回度量值arb
.map(度量值->{
Map inputMap=newhashmap();
inputMap.put(Utils.METRIC\u值,metricsValue);
返回inputMap;
});
}

我个人会选择选项2,因为它需要更少的代码。YMMV。

只是澄清一下:您想要一个包含多个条目的单一地图,还是一个包含每个条目的地图列表?具有多个条目的单个映射需要不同的键,但此处仅显示一个键(Utils.METRIC_值)。每个条目具有一个条目的映射列表@约翰·斯林克工作出色。Thx@约翰内斯林克
@Property
@Report(Reporting.GENERATED)
boolean statusReturnedFromApplyingRule2(
    @ForAll("generateRule") Rule rule,
    @ForAll @Size(min = 1, max = 10) Iterable<@From("generateInputMap") Map<String, Object>> elements
) {
    ...
}

@Provide
Arbitrary<Map<String, Object>> generateInputMap() {
    Arbitrary<Double> metricValueArb = Arbitraries.doubles()
                                                  .between(0, 50.0);

    return metricValueArb
        .map(metricsValue -> {
            Map<String, Object> inputMap = new HashMap<>();
            inputMap.put(Utils.METRIC_VALUE, metricsValue);
            return inputMap;
        });
}