3

I annotate my test methods like this:

@Test
@DatabaseSetup("/default_database_data.xml")
@ExpectedDatabase(value = "/expected_database_1.xml", assertionMode = NON_STRICT)

is it possible to manually perform the things that @DatabaseSetup and @ExpectedDatabase does:

@Test
public void test(){
  // DBUnit.setup("/default_database_data.xml");
  dao.insert(...);
  // DBUnit.expected("/expected_database_1.xml");
}

I made the syntax up, just to give you an idea of what I need: perform 2 setups and assertions in one unit test.

Roay Spol
  • 1,188
  • 1
  • 11
  • 17
  • why would you want to do two setups? Can you plz explain? – Eugene Apr 23 '13 at 07:09
  • DBUnit doesn't clean auto-generated IDs (sequences). In my *expected sets* I have tables with those IDs (and foreign keys pointing to those IDs). If the sequeces are not cleared, then the order of tests dictates the IDs of rows. **So either** I order the tests or clear the sequences or test foreign keys somehow different.. I don't know about any such solution, so I planned to have 1 test which calls non-test methods containing actual .. test code. Between the calls I need to reset & load DB. – Roay Spol Apr 23 '13 at 07:50
  • 2
    two things that might work, check this link. http://stackoverflow.com/questions/3813684/how-to-revert-the-database-back-to-the-initial-state-using-dbunit. And also this annotation: @DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD) on the class. – Eugene Apr 23 '13 at 07:57
  • `@DitirtiesContext` seems to make my tests run in order.. could this be true? – Roay Spol Apr 23 '13 at 08:09
  • not really. I just clears the application context. Read the documentation on it. – Eugene Apr 23 '13 at 08:12
  • I did, tests do pass but I didn't change the expected sets (they have IDs that match the order of tests) – Roay Spol Apr 23 '13 at 08:17
  • that could easy be a side-affect actually. But I'm glad the tests pass :) Nevertheless you should implement some cleaning method and run it after each test method. Ill post this as an answer.. – Eugene Apr 23 '13 at 08:23

1 Answers1

1

two things that might work, check this link.

Link

And also this annotation:

  @DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
Community
  • 1
  • 1
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • I just need to confirm if this actually works.. everywhere. And I'll accept :) – Roay Spol Apr 23 '13 at 09:07
  • I can't confirm this, but generally seems to work. This **is not a good solution**, so, dear visitors, pay attention when using this :( – Roay Spol Apr 29 '13 at 09:27