我遇到了一个问题。下面我粘贴了一些示例代码。
我的DefinedAddress类将从DB获取地址。
@Autowired
DBConnection connection;
public class DefinedAddress {
public String getAddress() {
List<? extends Address> addressList =connection.getAddresss("SELECT address FROM Address WHERE Id = 1;")
return addressList.get(0).location);
}
}我正在创建这个类的Junit,主要逻辑如下:我将模拟数据库调用方法connection.getAddresss,并将返回一个具有一个地址的列表。
public class DefinedAddressTest {
public static void main(String[] args) {
// Create a list
List<? extends Address> addressList = new ArrayList<>();
City city = new City();
city.setLocation("CurrentLocation");
addressList.add(city);//Compile time issue
//mock Defined Addrss getAddress() method to fetch the address
Mockit.when(connection.getAddresss("Query").return(addressList );
}
}现在我知道addressList不会允许像现在这样添加城市了?扩展Address,但我们不确定它将添加哪个对象(这很公平)
错误是:方法add(捕获#1-of?类型列表中的扩展地址)不适用于参数(城市)
那么,在这种情况下,我如何模拟调用并创建Junit测试呢?
发布于 2019-04-05 15:06:50
创建并返回一个列表。列表也是一个列表,您不需要测试用例的地址的子类型,因为您只访问一个地址字段。
https://stackoverflow.com/questions/55525346
复制相似问题