简体   繁体   中英

Is there any tools to record the Java objects for unit testing?

I want to write some unit tests and have to create or mock objects which are used in my program. I want to know if there is any tools that can capture and record the objects of my program during real running and use them for unit testing (Same as Selenium Recorder in End-to-End testing)?

I've never heard of a such tool. You will have to write your own.

You could rely on the persistence layer of your application:

  • to store objects at some specific point of the application execution,
  • to load these objects again in your tests.

You might be interrested by tools like databene-benerator which is

It is a framework for generating realistic and valid high-volume test data for your system under test

在此处输入图片说明

I had to refactor a large (production) codebase without any tests and unfortunately found nothing, so I had to develop a tool myself. The main objective was extensibility (the ability to plug in at every analysis stage) and quality (high code coverage by unit tests).

So anybody who is in search of such a tool might try out testrecorder . There are several ways how to generate tests from real usage. The easiest is probably using a callsite recorder :

try (CallsiteRecorder recorder = new CallsiteRecorder(ExampleObject.class.getDeclaredMethod("inc"))) {
  printTest(recorder.record(() -> {
    int doubleInc = exampleObject.inc().inc().get();
    System.out.println(doubleInc);
  }));
  printTest(recorder.record(() -> {
    int resetInc = exampleObject.reset().inc().get();
    System.out.println(resetInc);
  }));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM