Details
Description
If you have an inner class like this:
public class TodayFilterCommand implements FilterCommand {
private final DateWithoutTimeComparator comparator = new DateWithoutTimeComparator();
public class TodayFilterComparator implements Filter {
public boolean matches(Object cell)
}
}
In java you can only write a test that instantiates TodayFilterComparator by instantiating the outer class (e.g. TodayFilterCommand.new TodayFilterComparator) - XStream must use reflection and bypass this... in the test below we have to make "comparator" static to avoid the error, but its easy to miss this.
public void testWeCanMarshallObjectsWithInnerClassesAndOuterVariables()
{ TodayFilterCommand todayFilterCommand = new TodayFilterCommand(); TodayFilterCommand.TodayFilterComparator todayFilterComparator = todayFilterCommand.new TodayFilterComparator(); byte[] data = marshaller.marshal(todayFilterComparator); assertNotNull("Should have marshal data", data); TodayFilterCommand.TodayFilterComparator unmarshalledFilter = (TodayFilterCommand.TodayFilterComparator) marshaller.unmarshal(data); assertNotNull("Should have returned unmarshal data", unmarshalledFilter); assertFalse(unmarshalledFilter.matches(null)); }
No further comments, closing.