hibernate - junit noob - 类变量值在两种测试方法之间丢失
...我进行了一个简单的集成测试,所有测试方法都运行良好...但是... 我设置了一个类变量
int tempId;
供以下方法使用。 testSaveTag() 在成功执行时设置值(现在一切都是自动提交的),并且 testUpdateTag() 更新新创建的标签。
@Test
public void testSaveTag() {
Tag tag = new Tag();
tag.setDescription("Test Tag");
tempId = instance.saveTag(tag);
}
@Test
public void testUpdateTag() {
Tag tag = instance.getTag(tempId );
tag.setDescription("updated tag description!");
instance.updateTag(tag);
}
tempID 的值在这两种方法之间丢失。
所以我在想“执行此操作的正确方法是什么”,
......以及“为什么价值会丢失?”
提前致谢
... I got a simple integration test going and all test methods run fine ... BUT...
I set up a class var
int tempId;
for use by the following methods. testSaveTag() sets the value when successfully executed ( for now everything's auto committed) and the testUpdateTag() updates the freshly created tag.
@Test
public void testSaveTag() {
Tag tag = new Tag();
tag.setDescription("Test Tag");
tempId = instance.saveTag(tag);
}
@Test
public void testUpdateTag() {
Tag tag = instance.getTag(tempId );
tag.setDescription("updated tag description!");
instance.updateTag(tag);
}
The value of tempID gets lost between the the 2 methods.
So I'm thinking "What's the proper way to do this",
... and "why is the value lost?"
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JUnit 测试方法不应依赖于按特定顺序运行,并且应仅共享测试未更改的类变量。
在 testUpdateTag() 中,您可能必须创建并保存一个新标签才能获取 ID。或者有没有办法检索然后更新的标签?
顺便说一句,我希望你在某个时候添加断言......;-)
JUnit test methods should never depend on running in a certain order, and should only share class variables that are not changed by the tests.
In testUpdateTag() you might have to create and save a new tag just to get an ID. Or is there a way to retrieve a tag that you can then update?
BTW, I hope you are adding assertions at some point... ;-)
抱歉,我不是 junit 专家,但在大多数单元测试框架中,如何在每个单元测试方法之间发生测试系统的设置和拆卸。所以你的 tempId 可能无法在这个过程中幸存下来。
Sorry I am not the junit expert how ever in most unit test frame works there is a setup and tear down of the system of test which happens between each of your unit test methods. So your tempId is probably not surviving this process.