我有一個返回一個對象實現兩個接口泛型:如何處理多個界類型的類層次結構
public interface MyRepository<T extends A & B> extends JpaRepository<T, Long>
List<T> findAll();
...
使用Spring我注入的這些都實現到另一個類,但另外指定的存儲庫的泛型類型本身應該另外實現某個接口C
class X {
@Autowired
private List<MyRepository<? extends C> myCRepos;
注意,C是不延長&爲B接口,但是所有的具體實現我想要注入實現所有三個接口
class MyConcreteClass implements A, B, C
所以一個public interface ConcreteRepo extends MyRepository<MyConcreteClass>
正確的春天發現和注入,如預期X
類的List
。
如果我現在使用findAll
方法,則其返回類型由IDE推斷爲List<? extends C>
。
爲什麼返回類型不是寧可List<? extends A & B & C>
?
爲了測試我想嘲笑這個findAll
等返回正確類型的對象,但按照已經失敗:
List<? extends C> someTestList = new ArrayList<>();
someTestList.add(new MyConcreteClass()); //
// has error like
// The method add(capture#1-of ? extends C) in the type
// List<capture#1-of ? extends C> is not applicable for the arguments (C)
我應該怎麼解決這個問題?
最後,我想嘲笑這件事
Mockito.when(myRepo.findAll()).thenReturn(someTestList);
同樣的問題,應該是什麼類型someTestList
以及如何創建呢?
並Ç延伸A和B? – Ishnark
C號是A和B的分開 – Philipp