JUnit Test Case Dependency
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jeremy_Li
Posted On:   Monday, May 14, 2007 11:03 PM

A design question of junit, if I have an 'business order' package which I want to test as follow: Place order Enquire order Update order Cancel order In this case, I would like to have the three test cases doing in order. So I would have four test case method inside a junit class: public void testPlaceOrder() public void testEnquireOrder() public void testUpdateOrder() public void testCancelOrder() I created some 'global variables' for storing the order number created in testPlaceOrder. But I fou   More>>


A design question of junit, if I have an 'business order' package which I want to test as follow:




  1. Place order


  2. Enquire order


  3. Update order


  4. Cancel order



In this case, I would like to have the three test cases doing in order. So I would have four test case method inside a junit class:



public void testPlaceOrder()


public void testEnquireOrder()

public void testUpdateOrder()


public void testCancelOrder()



I created some 'global variables' for storing the order number created in testPlaceOrder. But I found that during the testEnquireOrder run, the order number is not found! I guess if junit would reinstantiate the class again.



Just want to know any design pattern in junit for doing such kind of 'dependency' test?

   <<Less

Re: JUnit Test Case Dependency

Posted By:   Robert_Lybarger  
Posted On:   Wednesday, May 16, 2007 08:56 AM

As far as test design goes, there are as many opinions as developers. If you want to make sure that "place order" works as expected, writing a unit test specifically for that is good. But the other tests will run independently of that test (plus or minus whatever happens in setup(), maybe). So if you then want to test the "enquire order" functionality, have that method first place an order and then include the unit test logic for the enquiry part. After all, you already have a unit test for placing the order in another method ... you can assume that the supporting business works as intended when you unit-test some other capability. Also feel free to use method names without the word "test" in them to hide them from the jUnit engine. For example:

public void testPlaceOrder() {
doPlaceOrder(arg values here);
// checks and assertions here for that order placement
}

public void testEnquireOrder() {
doPlaceOrder(arg values here);
// logic here to enquire
// checks and assertions here for that enquiry
}

private void doPlaceOrder(parameter list here) {
// logic to place an order here
}

Re: JUnit Test Case Dependency

Posted By:   Anonymous  
Posted On:   Wednesday, May 16, 2007 05:21 AM

These four test methods are not independent on each other so they are not testable independently. JUnit uses each testMethod as an independent test and resets (setup) the environment.


So I suggest trying this:


Order order;



public void setUp() {
order = .... // create an order
}

public void testPlaceOrder() {
// place order
// tests on initialized order
}

public void testEnquireOrder() {
// enquire ...
// tests
}

public void testUpdateOrder() {
// update ...
// tests
}

public void testCancelOrder() {
// cancel ...
// tests
}


About | Sitemap | Contact