【问题标题】:error: method addOutcome in class OutcomesTable cannot be applied to given types错误:类 OutcomesTable 中的方法 addOutcome 不能应用于给定类型
【发布时间】:2015-05-11 12:42:25
【问题描述】:

当我尝试使用 jbehave 结果表对匹配器进行 hamcrest 匹配时,我在构建 maven 时遇到编译时错误。

“错误:OutcomesTable 类中的方法 addOutcome 不能应用于给定类型”

请参考下面的示例代码。

public static <T> void method(T expected, T actual) {

        OutcomesTable outcomes = new OutcomesTable();
        List expectedList = (ArrayList)expected;
        List actualList = (ArrayList)actual;

        for(Object ExpObj : expectedList){
            outcomes.addOutcome("a success", actualList, containsInAnyOrder(ExpObj));
        }

        outcomes.verify();
}

请指出我在这里做错了什么。

【问题讨论】:

  • 这是“addOutcome”函数的类型错误。检查它的参数类型是否正确。
  • 查看文档,第三个参数应该是org.hamcrest.Matcher&lt;List&gt;...仔细检查您的containsInAnyOrder调用,我认为它的参数类型是罪魁祸首...
  • 感谢 Minits97 的支持

标签: java jbehave hamcrest


【解决方案1】:

JBehave 的通用函数public &lt;T&gt; void addOutcome(String description, T value, Matcher&lt;T&gt; matcher); 要求您在使用T 的两个参数中为T 提供相同的类型。例如如果您在第二个参数中传递了一个String value,那么您需要在第三个参数中提供一个Matcher&lt;String&gt;

在您的情况下,您将 List 作为第二个参数传递,但传递使用单个 Object 调用的 containsInAnyOrder(T... items) 函数的结果,这不会产生所需的 Matcher&lt;List&gt; 类型实例。

我不太确定,你想要做什么,但我认为你需要的是:

    outcomes.addOutcome("a success", actualList, Matchers.contains(ExpObj));

匹配器containsInAnyOrder(T... items) 仅对一个值没有真正意义,因为数组中的一个元素仅存在一种排列(顺序)。 :)

你也可以像这样编译你的代码:

    outcomes.addOutcome("a success", actualList, Matchers.containsInAnyOrder(Arrays.asList(ExpObj)));

,但我认为这不是你需要的。

希望我说的很清楚,并且可以提供帮助。

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多