Search code examples
javajbehavehamcrest

error: method addOutcome in class OutcomesTable cannot be applied to given types


When I am trying to hamcrest matcher with jbehave outcome table I am getting below compile time error at the time of maven build.

"error: method addOutcome in class OutcomesTable cannot be applied to given types"

please refer below sample code.

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();
}

please suggest what I am doing wrong here.


Solution

  • JBehave's generic function public <T> void addOutcome(String description, T value, Matcher<T> matcher); expects that you provide the same type for T in both parameters that use T. e.g. If you pass a String value in your second parameter, then you need to provide a Matcher<String> in the third parameter.

    In your case, you pass a List as the 2nd parameter, but pass the result of containsInAnyOrder(T... items) function called with a single Object, which will not produce the desired instance of a Matcher<List> type.

    I'm not pretty sure, what you are trying to do, but I think what you need is:

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

    The Matcher containsInAnyOrder(T... items) does not really make sense for only one value, as there exists only one permutation(order) of one element in an array. :)

    You can also make your code compile like this:

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

    , but I think that's not what you need.

    Hope I was clear, and could help.