2

Is there a Java function to Generate Mock data CODE by taking a Java Object? The output uses the existing setter but not constructor. Say such a function named void Generate(Class<T> klazz)

Generate(person);//person is a POJO with data in it.

This will sysout the following in stdout.

Person mockPerson = new Person;
mockPerson.setName("name");
mockPerson.setAge(1);

I believe this may take advantage of Java Reflection.

  • what is the use case you are looking to address? – Dima Mar 31 '15 at 18:34
  • 1
    I am trying to mock data using Mockito and the data needs to be exactly like the actual data in runtime inside other project. I am using breakpoint to look at the actual data in the Java Object and copy it to the mock Java Object. If you know what I mean. – Arnolddddddd Mar 31 '15 at 18:39
  • What I don't understand is why not just use the actual object? person = new Person(); person.setName("foo"); person.setAge(bar); – Dima Mar 31 '15 at 18:51
  • 1
    Because it is coming from the other project. – Arnolddddddd Mar 31 '15 at 18:52
  • So what? What exactly prevents you from instantiating it? I mean, you *can* do something like: person = mock(Person); when(person.getName()).thenReturn("foo"); when(person.getAge()).thenReturn(1); But I don't see *why* you would want to do something like this instead of simply instantiating the object. – Dima Mar 31 '15 at 18:53
  • In the other project. There is no object initialization code for me to copy over since it's getting data from database and the object itself is instantiated by dependency injection. I will be mocking hundreds of objects. Better let Java generate the code for me. – Arnolddddddd Mar 31 '15 at 19:09
  • Where will the generated code get the data from? – Dima Mar 31 '15 at 19:39
  • The object itself in runtime. – Arnolddddddd Mar 31 '15 at 19:42
  • I still don't understand. If you already have an object, why do you need to mock it??? – Dima Mar 31 '15 at 22:44
  • @Dima: Its relevant in following scenario: Maybe a user finds a bug in my code. He sends a description how to reproduce. I track the bug down to object Person. Now I want to write a unit test for this test data (to prevent further bugs). Since the Person Class could be complex It would be fine, that some tool could serialize the complete state of the test object to file. – CoronA Apr 01 '15 at 05:35
  • I think this question is simlar, right? http://stackoverflow.com/questions/16336224/serialize-object-for-unit-testing , however they did not find a solution – CoronA Apr 01 '15 at 05:44
  • @StefanA do you mean the data would be included into the report from the user (otherwise, I still don't understand where it would come from to get into the "tool")? If you make your class Serializable, you could store its state into a binary file to restore it later. Is this what you are looking for? – Dima Apr 01 '15 at 13:28
  • My thoughts: I know the location of the error and I know the object that causes the error, I dont know the state of this object. So I want to serialize this object (after logging the error). I could indeed use the java serializer. But reloading a serialized object in a unit test is a problem, since it gets invalid if I change this class (which is probable if it contains bugs). So the serialization should contain a sequence of constructors/setters that lead to the object. It is human readable, insertable into a unit test and even changeable, if the class changes. – CoronA Apr 01 '15 at 15:05
  • @StefanA That's exactly my problem. – Arnolddddddd Apr 01 '15 at 17:31

0 Answers0