Why doesn't this code attempting to use Hamcrest's hasItems compile?

前端 未结 9 1855
耶瑟儿~
耶瑟儿~ 2020-12-03 00:49

Why does this not compile, oh, what to do?

import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.hasItems;

ArrayList<         


        
9条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 00:50

    ArrayList expected = new ArrayList();
    expected.add(1);
    expected.add(2);
    hasItems(expected);
    

    hasItems(T..t) is being expanded by the compiler to:

    hasItems(new ArrayList[]{expected});
    

    You are passing a single element array containing an ArrayList. If you change the ArrayList to an Array, then your code will work.

    Integer[] expected = new Integer[]{1, 2};
    hasItems(expected);
    

    This will be expanded to:

    hasItems(1, 2);
    

提交回复
热议问题