Results 1 to 6 of 6

Thread: JUnit Tests - Inconsistent

  1. #1

    Default JUnit Tests - Inconsistent

    Hi - I'm not sure if this is the correct forum.

    I'm sure this has a very simple answer, just can't find it!

    I have JUnit tests which work when run independently but fail when run as a batch, is there a setting or class I need to extend to make sure all my spring beans are in their 'base state' before the test is run?

    Thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Norway
    Posts
    30

    Default

    Do you extend TestCase directly, or do you extend one of the Spring test classes?

    If you extend for instance AbstractDependencyInjectionSpringContextTests, you can override the onTearDown() method and use setDirty(true). However, this will make your tests considerably slower.

  3. #3

    Default

    Hi toverlier - thanks for the quick reply - I've tried what you suggested but it doesn't seem to work. No idea why. I changed the extension from TestCase to AbstractDependencyInjectionSpringContextTests and did :

    HTML Code:
    @Override
    protected void onTearDown() throws Exception {
        super.onTearDown();
        setDirty(true);
    }
    The test still fails when running all the test in the same class and passes when run individually.

  4. #4
    Join Date
    Jan 2008
    Location
    Norway
    Posts
    30

    Default

    Well, it doesn't do you any good just to change from TestCase to AbstractDependencyInjectionSpringContextTests. You have to let spring-test handle the context for you. I suggest you check out the documentation at http://static.springframework.org/sp...e/testing.html

  5. #5

    Default

    Thanks toverlier, very useful link, everything is working as expected.

    Code now reads :

    HTML Code:
    @ContextConfiguration(locations={"/mySpringBeans.xml"})
    public class MyTest extends  AbstractJUnit38SpringContextTests {
        
       @DirtiesContext
        public void testA{ ...  }
    }

  6. #6
    Join Date
    Jun 2008
    Posts
    15

    Default

    spring unit test can not insert data to database

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •