com.google.common.testing.junit3
Class TearDownTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by com.google.common.testing.junit3.TearDownTestCase
All Implemented Interfaces:
TearDownAccepter, junit.framework.Test

public abstract class TearDownTestCase
extends junit.framework.TestCase
implements TearDownAccepter

A base class for test cases that require reliable tear-down.

JUnit's concept of TestCase.tearDown() works fine for strict "unit tests" (which is what the authors of JUnit "want" you to write with it), but it turns out that for anything even slightly more complex, it has severe limitations, including:

TearDownTestCase solves all these problems. Whenever you create an object which must be removed later, or make any persistent change to a shared resource that must be reverted later, simply pop a TearDown item onto the stack immediately:
   addTearDown(new TearDown() {
     public void tearDown() throws Exception {
       // cleanup logic here
     }
   });
 

If you are writing a piece of test infrastructure, not a test case, and you want to be sure that what you do will be cleaned up, simply require your caller to pass in an active instance of TearDownAccepter, to which you can add your TearDowns.

Please see usage examples in TearDownTestCaseTest.

Author:
Kevin Bourrillion

Field Summary
(package private)  TearDownStack stack
           
 
Constructor Summary
TearDownTestCase()
          Creates a TearDownTestCase with the default (empty) name.
TearDownTestCase(java.lang.String name)
          Creates a TearDownTestCase with the specified name.
 
Method Summary
 void addTearDown(TearDown tearDown)
          Registers a TearDown implementor which will be run during tearDown()
 void runBare()
           
protected  void setUp()
           
protected  void tearDown()
           
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

stack

final TearDownStack stack
Constructor Detail

TearDownTestCase

public TearDownTestCase()
Creates a TearDownTestCase with the default (empty) name.


TearDownTestCase

public TearDownTestCase(java.lang.String name)
Creates a TearDownTestCase with the specified name.

Method Detail

setUp

protected void setUp()
              throws java.lang.Exception
Overrides:
setUp in class junit.framework.TestCase
Throws:
java.lang.Exception

addTearDown

public final void addTearDown(TearDown tearDown)
Registers a TearDown implementor which will be run during tearDown()

Specified by:
addTearDown in interface TearDownAccepter

tearDown

protected final void tearDown()
Overrides:
tearDown in class junit.framework.TestCase

runBare

public final void runBare()
                   throws java.lang.Throwable
Overrides:
runBare in class junit.framework.TestCase
Throws:
java.lang.Throwable


Copyright © 2011 Google. All Rights Reserved.