hibernate - junit noob - class variable value lost between 2 test methods
... 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
publi开发者_Python百科c 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
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... ;-)
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.
精彩评论