andrej
andrej

Reputation: 176

Testing classes implementing java.lang.Serializable to make sure any changes are backwards compaitible

I'm trying to use OpenHFT/Chronicle-Map and MapDB as to persist a simple String -> Foo database.

Foo may evolve over time, I'm wondering what testing strategies people have adopted to ensure that any older version of Foo persisted will be loadable going forward.

I was thinking of starting serialVersionUID at 1 for each specific class, storing each particular serialized version in a file I check in and have a test, which ensures I can read all of those files back and the values are what I expected.

It seems a little manual though.

Thanks

Upvotes: 0

Views: 63

Answers (1)

lance-java
lance-java

Reputation: 27976

You could automate the process of creating the serialized files

  1. Store each version of Foo.java under src/test/resources
  2. The test case could compile the .java to a .class at runtime using the JavaCompiler
  3. Use a disposable ClassLoader to load each .class version
  4. Instantiate / populate (possibly using reflection) / serialize an instance

Once this is in place, the only maintenance wil be

  1. The source files for each version
  2. A population strategy

Upvotes: 1

Related Questions