泛型 [core Java 閱讀筆記]
題記:單元測試的過程中,遇到泛型mock的問題;重新溫習一遍,閱讀(core java 泛型)
xmind格式(可下載) :整理過程中,記錄為xmind格式
單元測試遇到的問題,簡化后如下:
1 public List<? extends Date> getDateT() {
2 return null;
3 }
4 public List<Date> getDate() {
5 return null;
6 }
7 public void mockGetDate() {
8 TestMain main = mock(TestMain.class);
9 when(main.getDate()).thenReturn(new ArrayList<Date>()); //編譯OK
10 /*
11 * The method thenReturn(List<capture#2-of ? extends Date>) in the type
12 * OngoingStubbing<List<capture#2-of ? extends Date>>
is not applicable for the arguments (ArrayList<Date>)
13 */
14 when(main.getDateT()).thenReturn(new ArrayList<Date>()); //編譯錯誤
15 when(main.getDateT()).thenReturn(new ArrayList<Timestamp>()); //編譯錯誤
16 when(main.getDateT()).thenReturn(new ArrayList<Object>()); //編譯錯誤
17 when(main.getDateT()).thenReturn(new ArrayList()); //編譯OK
18 }
2 return null;
3 }
4 public List<Date> getDate() {
5 return null;
6 }
7 public void mockGetDate() {
8 TestMain main = mock(TestMain.class);
9 when(main.getDate()).thenReturn(new ArrayList<Date>()); //編譯OK
10 /*
11 * The method thenReturn(List<capture#2-of ? extends Date>) in the type
12 * OngoingStubbing<List<capture#2-of ? extends Date>>
is not applicable for the arguments (ArrayList<Date>)
13 */
14 when(main.getDateT()).thenReturn(new ArrayList<Date>()); //編譯錯誤
15 when(main.getDateT()).thenReturn(new ArrayList<Timestamp>()); //編譯錯誤
16 when(main.getDateT()).thenReturn(new ArrayList<Object>()); //編譯錯誤
17 when(main.getDateT()).thenReturn(new ArrayList()); //編譯OK
18 }
仍沒理解,哪位大仙,能幫我解釋下 ?
posted on 2012-03-08 21:05 石建 | Fat Mind 閱讀(336) 評論(0) 編輯 收藏