views:

23

answers:

2

... 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

+1  A: 

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.

Aaron Fischer
... hm, that makes sense. Thanks.
vector
+1  A: 

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... ;-)

Rodney Gitzel
That's the ticket! Ehm, of course, yes :-) Duh, I can combine those 2 test!
vector