Page 4 of 4 FirstFirst ... 234
Results 31 to 35 of 35

Thread: Junit4

  1. #31
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default SpringJUnit4ClassRunner and abstract base support classes

    Hi Mujahid,

    Quote Originally Posted by mujahid View Post
    Is there any support in Spring 2.0 to write test cases using Junit4. Is there a similar class to AbstractDependencyInjectionSpringContextTests which provides support for tests that depend on a Spring context.
    The Spring TestContext Framework provides a custom runner for JUnit 4.4 called SpringJUnit4ClassRunner. You annotate your classes as follows to get it working:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    public class SimpleTest {
    
    	@Test
    	public void testMethod() {
    		// execute test logic...
    	}
    }
    As an alternative, we also provide abstract base test classes which you can directly extend. Once the Spring 2.1 M4 release is officially out, take a look at the following classes:

    • AbstractJUnit4SpringContextTests
    • AbstractTransactionalJUnit4SpringContextTests


    For JUnit 3.8 support with the new Spring TestContext Framework, we provide the following analogous classes:

    • AbstractJUnit38SpringContextTests
    • AbstractTransactionalJUnit38SpringContextTests


    cheers,

    Sam

  2. #32
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default @ExpectedException

    Hi belugabob,

    Quote Originally Posted by belugabob View Post
    +1 on the initial request.

    For me, one of the main attractions of using JUnit4 is that by using declarative exception handling (Instead of the usual empty catch block) the coverage readings of your test code are a more accurate representation of what you'd want.

    The Junit4 way...

    Code:
    @Test(expected = IllegalParameterException.class)
    public void testExceptionThrown(){
        objectUnderTest.invokeExceptionThrowingMethod
    }
    You might not have noticed it, but Spring 2.0 actually already offered similar support for JUnit 3.8 based tests with the @ExpectedException annotation, which you can use if you extend AbstractAnnotationAwareTransactionalTests. Here's an example:

    Code:
    @ExpectedException(SomeBusinessException.class)
    public void testProcessRainyDayScenario() {
        // some logic that should result in an Exception being thrown
    }
    As of Spring 2.1 M4 you can use @ExpectedException with both the JUnit 3.8 and JUnit 4.4 support in the Spring TestContext Framework. Though we recommend using JUnit's out-of-the-box support (i.e., @Test(expected = SomeBusinessException.class)) when developing against JUnit 4+.

    cheers,

    Sam

  3. #33

    Default

    Sam,

    Could you provide a link for the new documentation 'Spring TestContext Framework' - I tried the 2.1 Reference Documentation, but this seems to be 2.1 M3, where can I find M4 docs ?

  4. #34
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    Hi PeteTh,

    Quote Originally Posted by PeteTh View Post
    Could you provide a link for the new documentation 'Spring TestContext Framework' - I tried the 2.1 Reference Documentation, but this seems to be 2.1 M3, where can I find M4 docs ?
    Spring 2.1 M4 will be out by the end of this week, at which point you will be able to access the reference manual both online and via the distribution. If you are eager to have an earlier look, however, you can download a 2.1 M4 snapshot from CVS HEAD at SourceForge.

    cheers,

    Sam

  5. #35
    Join Date
    May 2006
    Location
    Crawley, UK
    Posts
    105

    Thumbs up

    Sam,

    Thanks for the info - sounds very interesting!

    I did notice the @ExpectedException annotation - but only about 2 weeks ago, whilst reviewing my testing practices.

    Regards,

    Bob
    If you didn't learn anything today, you weren't paying attention!

Posting Permissions

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