pio*_*rek 15 java generics mockito
是否可以使用签名模拟(使用mockito)方法Set<? extends Car> getCars()而不使用supress警告?我试过了:
XXX cars = xxx;
when(owner.getCars()).thenReturn(cars);
Run Code Online (Sandbox Code Playgroud)
但不管我怎么声明cars我总是得到一个编译错误.例如,当我宣布这样的时候
Set<? extends Car> cars = xxx
Run Code Online (Sandbox Code Playgroud)
我得到标准的通用/ mockito编译错误
The method thenReturn(Set<capture#1-of ? extends Car>) in the type OngoingStubbing<Set<capture#1-of ? extends Car>> is not applicable for the arguments (Set<capture#2-of ? extends Car>)
Run Code Online (Sandbox Code Playgroud)
Tom*_*sky 32
使用doReturn-when备用存根语法.
被测系统:
public class MyClass {
Set<? extends Number> getSet() {
return new HashSet<Integer>();
}
}
Run Code Online (Sandbox Code Playgroud)
和测试用例:
import static org.mockito.Mockito.*;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
public class TestMyClass {
@Test
public void testGetSet() {
final MyClass mockInstance = mock(MyClass.class);
final Set<Integer> resultSet = new HashSet<Integer>();
resultSet.add(1);
resultSet.add(2);
resultSet.add(3);
doReturn(resultSet).when(mockInstance).getSet();
System.out.println(mockInstance.getSet());
}
}
Run Code Online (Sandbox Code Playgroud)
无需错误或警告抑制
| 归档时间: |
|
| 查看次数: |
10727 次 |
| 最近记录: |