我有一个返回一个对象实现两个接口泛型:如何处理多个界类型的类层次结构
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