简体   繁体   English

是否有用于记录Java对象以进行单元测试的工具?

[英]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)? 我想知道是否有任何工具可以在实际运行期间捕获并记录程序的对象并将其用于单元测试(与端到端测试中的Selenium Recorder相同)?

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 您可能会被诸如databene-benerator之类的工具所困扰

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 . 因此,任何正在寻找这种工具的人都可以尝试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);
  }));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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