0

I was wondering if it possible to save instance of an object during runtime and use that instance latter on for testing.

My case is that my application makes request to server, parses json response to objects and latter on I want to use some of these instances for junit testing.

I don't want to serialize them.

For example I have Set containing strings "1","2","3". At given point i want to save an instance of it. Program goes, expects all the fields of the object, makes new instance, initialize it with current values and stores it as MySet or something like this and it contains the given strings.

I hope I was clear enough.

Aboikliev
  • 1
  • 3
  • 2
    "I don't want to serialize them" -- do you just mean you don't want to implement `Serializable`? Because persisting object states for later use is pretty much the definition of serialization. – Henry Keiter Sep 19 '13 at 16:44

3 Answers3

0

There are plenty of ways even if you dont want to use serialization. This question has several of them listed. Most of them persist the object to the disk in some form, or use a database. XStream is pretty quick to set up and easy to use.

Community
  • 1
  • 1
Zavior
  • 6,182
  • 2
  • 25
  • 36
0

If you need a quick and messy way to save objects, Object streams are a easy way to do this.

See http://docs.oracle.com/javase/6/docs/api/java/io/ObjectOutputStream.html and http://docs.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html.

Afshin Moazami
  • 2,003
  • 5
  • 32
  • 54
tcooc
  • 18,644
  • 3
  • 34
  • 53
0

Automated tests use Data Providers to provide different test data. After the object gets created, log it out printing the value of each member. Use these logged values to re-create the required object from within a data provider when testing

Trey Jonn
  • 340
  • 3
  • 17