Is it ok to use Java's serialization in automated tests?

Hi everybody, greetings from Spain!
I need to create automated tests for a system which basically do 2 things:

  1. Load file, parses it and creates a Java Object representing the original file (This takes 4s)
  2. Takes that new object and do some calculation (This takes less than a second)
    In particular, in the tests, I want to focus in the second part (calculation), so Im looking for ways of bypassing the file parsing which will be done by other tests.
    The question is… Do you think that parsing the file and serializing the object obtained is a good solution? (The test will use serialized version instead of the original file)
    For me it makes sense because you isolate the problem (I know you need to take care of those serialized objects)
    Do you have any other approaches for this kind of tests?
    Thanks a lot!

I would be interested in knowing if there are other approaches as well.

For what it is worth, what you describe is pretty much what I would do.

1 Like

Would it be possible to do the calculation from the data in the file without the serialization and then compare the results with what you get from the parsed file?

1 Like

hey Moni, thanks for your new question :grinning:
The thing is that the step 1 loads the file and transforms it to the object of our domain and we need that object to be passed to step number 2, which does the calculation.
Normally, I would use some stub for the object, but it is huge and complex, so that is why I’m thinking about serializing it or any other way of speeding up the first step.

1 Like

Sometimes slow but robust is always a healthy choice as long as there is not a business constraint you are not mentioning… Wow 4 seconds to serialize a file, is that a size or a conversion problem, and what are you using the object for afterwards. I imagine if you gave the serialization more horsepower, would that speed it up? Of is it not practical to offload that piece? I’m trying to understand if your question is about serialization cost being a good solution or performance being a problem, because your downside or risk is not clear to me.

1 Like

You can use Java serialization but what about storing as JSON? It is readable and there are good libraries available for it.

1 Like

Hi Conrad, thanks for your answer!
Sorry if I haven’t explained it properly:
the serialization does not take 4s: the original code does NOT use serialization, it has a huge method that reads, parses and creates a object in memory (step 1). That takes 4s
I’m not interested in that part for my test: I just need that object (or a test double) as input in order to test step 2.
As this object is really complex (difficult to mock) I thought that I could use serialization: execute step 1, serialize the object (the test will use serialized object instead of executing the “real” code)
I need to use some mechanism like that because I need to repeat the same test with different inputs (different files for step 1) and I don’t want each test to start with that 4s burden.
The downside of using serialization is that if you do changes in the piece of code of step 1, you might need to re-serialize in order to work , so I need to maintain those serialized objects (plus serialization has bad press)
I hope is more clear now :grinning:

2 Likes

Hey Martin,
thanks for that!
I will check if it works: I need to convert the json to an object

Ah, serialization gets my vote for your use case.

As always good to have technical questions in the club. Making an object double makes for less pain than mocking. I just wish I was more of a Java expert and lover to be more able to weigh in with relevant experiences.