序列化对象以进行单元测试 [英] serialize object for unit testing

查看:84
本文介绍了序列化对象以进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在单元测试中,我需要有一个对象,其中的所有50个字段都设置有一些值。
我不想手动设置所有这些字段,因为这需要时间和烦人的事。

Assuming in a unit test I need to have an object all of 50 fields of which are set up with some values. I don't want to manually set up all this fields, as it takes time and annoying...

我需要以某种方式获得一个实例,其中字段由一些非空值初始化。

Somehow I need to get an instance where all the fields are initialized by some not-null values.

我有一个想法-如果我将调试一些代码,在某个时候我将得到一个有效的实例该对象已设置了一些数据-我只是将其序列化到磁盘上。

and I had an idea - what if I will debug some code, at some point i will get a working instance of this object with some data set up - and I just will serialize it to the disk.

然后,我将该文件放入test-resources文件夹,在单元测试中,它将从该位置反序列化。

Then I will put this file to test-resources folder, and in unit tests will just deserialize it from that location.

听起来可行..合理吗?还有其他想法,或者您将如何做?

Sounds feasible.. and reasonable? Is there any other idea, or how would you do that?

UPD:我同意在这种情况下序列化不是很好。 1)保存的对象不是人类可读的2)版本可能会更改(极不可能),并且我认为这不是什么大问题...
那么,也许有任何可读的,易于序列化的格式吗?

UPD: I agree serialization is not good in that case. 1) Saved object is not human readable 2) Version could change (highly unlikely) and is not a big problem I believe... So, maybe are there any readable easy-for-serialization formats?

理想情况下,我想实际生成源代码。因为它是一个Java Bean,所以所有的getter / setter都在那里。为什么不在运行时通过给定对象生成setter调用集?

Ideally I would like to have actually source code to be generated. Because it is a java bean, all the getters/setters are there. Why not generate the set of setters calls over the given object in runtime?

推荐答案

此问题已解决:
https://github.com/bbottema/lorem-ipsum-objects

这篇关于序列化对象以进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆