我正在不愉快地处理一个由某人定义的以下接口
public Map<?, ?> getMap(String key);
我正在尝试编写使用此接口的单元测试。
Map<String,String> pageMaps = new HashMap<String,String();
pageMaps.put(EmptyResultsHandler.PAGEIDENT,"boogie");
pageMaps.put(EmptyResultsHandler.BROWSEPARENTNODEID, "Chompie");
Map<?,?> stupid = (Map<?, ?>)pageMaps;
EasyMock.expect(config.getMap("sillyMap")).andReturn(stupid);
编译器出问题了。
The method andReturn(Map<capture#5-of ?,capture#6-of ?>) in the type IExpectationSetters<Map<capture#5-of ?,capture#6-of ?>> is not applicable for the arguments (Map<capture#7-of ?,capture#8-of ?>)
如果直接使用
pageMaps
,会提示我:The method andReturn(Map<capture#5-of ?,capture#6-of ?>) in the type IExpectationSetters<Map<capture#5-of ?,capture#6-of ?>> is not applicable for the arguments (Map<String,String>)
如果我将pageMaps
设为Map<?,?>
,那么我就无法在其中放置字符串。
The method put(capture#3-of ?, capture#4-of ?) in the type Map<capture#3-of ?,capture#4-of ?> is not applicable for the arguments (String, String)
我看过一些客户端代码,其中包含了丑陋的未经检查的转换,例如
@SuppressWarnings("unchecked")
final Map<String, String> emptySearchResultsPageMaps = (Map<String, String>) conf.getMap("emptySearchResultsPage");
我怎样将数据放入一个 Map<?,?>
中,或将我的 Map<String,String>
转换为 Map<?,?>
?
pageMaps
,会发生什么?EasyMock.expect(config.getMap("sillyMap")).andReturn(pageMaps);
- JB Nizet