Question

I am using Apache's GenericObjectPool to create the pool of objects of my class.

I have web application. Once my request is processed, I am putting the the object back to the pool without any issues. In the code i am keeping the object back to the pool in finally block.

My question is, in my JUnit test cases also i am getting the object from the pool and using it. Do I really need to keep the object back to the pool after my test case execution is completed?

@Test
public void testMethod(){

  try{
     //some code after getting the object from the pool
  }catch(Exception e){

   }finally{

    //call the logic to put the object back to the pool here
   }
}

Does it really required to keep the objects back to the client in test cases as mentioned in the finally block above?

Was it helpful?

Solution

Yes, you definitely have to because of the following reasons:

  • in case you have a lot of test cases - eventually your pool will be empty and test start to crash
  • you test cases may be a guideline for your API users - they may be confused with such code
  • it's best practice and should be done automatically

OTHER TIPS

Whether you actually need to depends on the type of object. If there's a pool of database connections, for instance, then you will quickly run out unless you return them to the pool. Failing to return other types of objects may quickly consume all the memory available to the test program. In general, the answer is yes, you need to. In some cases it may be open to get away with not doing it, but even in those cases, I'd consider it bad style.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top