我不开心处理与以下把东西在地图<?,?>或转换地图<String,字符串>到地图<?,?>
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<?,?>
?
这里有一种混淆:你尝试说服编译器愚蠢是一个带有2个类型参数的Map *你不关心*,但是,尽管如此,你仍然使用Map类型的Map 。但是你应该做的是测试这个接口适用于你需要的任何类型的组合。 –
Ingo
2013-02-24 10:17:16
如果让它返回'pageMaps',会发生什么? 'EasyMock.expect(config.getMap(“sillyMap”))。andReturn(pageMaps);' – 2013-02-24 10:20:21
我只关心它作为一个Map来进行这个测试,但这是无关紧要的。如果我无法获取数据,我不知道如何“测试该接口适用于我需要的任何类型的组合”。如果我使地图完全通用,put()拒绝工作。 –
2013-02-24 10:23:25