HashMap not Serializable
The exception message tells you exactly what the problem is: you are trying to serialize an instance of class SimpleSerializationTest
, and that class is not serializable.
Why? Well, you have created an anonymous inner class of SimpleSerializationTest
, one that extends HashMap
, and you are trying to serialize an instance of that class. Inner classes always have references to the relevant instance of their outer class, and by default, serialization will try to traverse those.
I observe that you use a double-brace {{ ... }}
syntax as if you think it has some sort of special significance. It is important to understand that it is actually two separate constructs. The outer pair of braces appearing immediately after a constructor invocation mark the boundaries of the inner class definition. The inner pair bound an instance initializer block, such as you can use in any class body (though they are unusual in contexts other than anonymous inner classes). Ordinarily, you would also include one or more method implementations / overrides inside the outer pair, either before or after the initializer block.
Try this instead:
public void testHashMap() throws Exception {
Map<String, String> hmap = new HashMap<String, String>();
hmap.put(new String("key"), "value");
// ...
}