私は不幸なことに、誰かが次のように定義したインターフェースを扱っています
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<?,?>
ですか?