java - How to run tearDown type method for a specific test in JUnit class with multiple tests? -


i have junit testcase class multiple test methods in ( requirement , don't want create separate class each test.)

i wanna create teardown type method each test method , run test. not test.

my problem , in many tests insert record in database, test , delete after test. but, if test fails mid way , control don't reaches till end dummy record ain't deleting.

i think 1 teardown() allowed 1 class, , teardown() don't know object/record created or inserted , delete!!!

i want create teardown() or @after method 1 specific test. finally{} in java each method.

for eg:

 public class testdummy extends testcase {          public void testsample1(){                      insertsomedata1();           assertfalse(true);          runteardown1();     }      public void testsample2(){                      insertsomedata2();          assertfalse(true);                      runteardown2();      }        public void runteardown1(){       deletedummydatafromtestsample1....      }        public void runteardown2(){       deletedummydatafromtestsample2....      }  } 

here control never go runteardown1() or runteardown2() , don't 1 common teardown() because won't know data inserted , thats specific each method.

it seems test relies on fixed database, , future tests break if current test breaks. i'd recommend not focus on particular problem (a test-specific teardown method runs each test), main problem - borken tests. before test run, should work clean database, , should case each test. right now, first test has relationship second (through database).

what right approach recreate database before each test, or @ least reset basic state. in case, you'll want test this:

public class testdummy {      // code runs (once) when test class run.     @beforeclass     public void setupdatabase() {         // code creates database schema     }      // code runs after tests in class run.     @afterclass     public void teardowndatabase() {         // code deletes database, leaving no trace whatsoever.     }      // code runs before each test case. use to, example, purge     // database , fill default data.     @before     public void before() {      }      // can use method delete test data inserted test method too.     @after     public void after() {      }      // tests themselves, should able assume database     // in correct state, independent previous or next test cases.     @test     public void testsample2() {        insertsomedata();        asserttrue(somedata, isvalid());     } } 

disclaimer: junit 4 tests (using annotations), might not right annotations, might not right answer(s).


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -