Input.getClass () seems to return ModelConvertersFacadeTests
This is not true. Your stacktrace says this is a class:
com.example-company.test.unit.util.converters.ModelConvertersFacadeTests $ 1
Pay attention to $1 at the end. This means that your class is anonymous (it does not have a proper name) of the inner class.
this$0 , which we see in your screenshot, is just a link to an external class.
Each time you make new SomeClass() { ... } , you create an anonymous inner class.
The initialization of the double bracket has nothing to do with this. Each time you use double-bracket initialization, you also create an anonymous inner class.
Solution by searching the map in different ways
Your Map has a mapping for Right.class and Location.class , but it doesn't have a mapping for subclasses of these two classes.
static { modelConverterBacking.put(Right.class, new RightConverter()); modelConverterBacking.put(Location.class, new LocationConverter()); };
What you could do (not to say that this is the best approach) is to scroll through the keys of your card and check:
mapKey.isAssignableFrom(input.getClass())
When this returns true, you know that you either have a mapKey class, or you have a subclass of it.
Instead of iterating over the map keys, you can also iterate over the superclasses and implemented interfaces of the object you are going to, and search for modelConverterBacking.get for each of them. The effect will be the same.
Solution without using an anonymous inner class
Your current code is:
final Location stub = new Location() { { setLocationName(""); } };
If you do this:
final Location stub = new Location(); stub.setLocationName("");
Then you will not create an anonymous inner class and therefore will not have this problem.
However, even if you just do this:
final Location stub = new Location() {}; stub.setLocationName("");
Then you have an anonymous inner class that will cause problems for you.
Very important do not mix the two classes ModelConvertersFacadeTests$1 and ModelConvertersFacadeTests .