Tags: , ,

JUnit 4 in 60 Seconds

I played with JUnit 4 library this weekend and here is the short introduction to it:

  1. @Test
    Mark your test cases with @Test annotations. You don’t need to prefix your test cases with “test”.  In addition, your class does not need to extend from “TestCase” class.

  2. @Before and @After
    Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case.

  3. @BeforeClass and @AfterClass
    Use @BeforeClass and @AfterClass annotations for class wide “setup” and “tearDown” respectively. Think them as one time setup and tearDown. They run for one time before and after all test cases.

  4. Exception Handling
    Use “expected” paramater with @Test annotation for test cases that expect exception. Write the class name of the exception that will be thrown.

  5. @Ignore
    Put @Ignore annotation for test cases you want to ignore. You can add a string parameter that defines the reason of ignorance if you want.
  6. Timeout
    Define a timeout period in miliseconds with “timeout” parameter. The test fails when the timeout period exceeds.

  7. New Assertions
    Compare arrays with new assertion methods. Two arrays are equal if they have the same length and each element is equal to the corresponding element in the other array; otherwise, they’re not.

    public static void assertEquals(Object[] expected, Object[] actual);
    public static void assertEquals(String message, Object[] expected, Object[] actual);

  8. JUnit4Adapter
    Run your Junit 4 tests in Junit 3 test runners with Junit4Adapter.

Happy coding. :D

Share and Enjoy:
  • del.icio.us
  • description
  • StumbleUpon
  • Digg
  • Technorati
  • Facebook
  • Reddit
  • TwitThis
  • Ma.gnolia
  • Mixx
  • Slashdot
  • Google
  • YahooMyWeb
  • Pownce

Related Posts

  • No Related Posts

18 Responses to “JUnit 4 in 60 Seconds”


  1. 1 MJ

    Nice one, thanks!

  2. 2 westlakken

    sometimes I’m not sure if I really exist

  3. 3 Peter Andersen

    If the static import has been shown in 1. the example contain the full picture.

    import static junit.framework.Assert.*;

    or

    import static junit.framework.Assert.assertEquals;

    Good 60 sec. guide though

    /Peter

  4. 4 Abdullah Cetin CAVDAR

    Thanks for reminding the static imports Peter. :D

  5. 5 Stephen

    westlakken, maybe you are just someone else’s reflection.

    nice junit guide. appreciate!

  6. 6 BLK

    Nice… is great for me, and very easy, thanks..

  7. 7 Naveen

    Great way to explain in less time

  8. 8 timothy

    clear and concise explanation! thank you.

  9. 9 Aley Raza

    Nice tutorial

  10. 10 santhosh

    Hey appreciate yor help regarding this
    I am moved from a novice user to some kind of a basic user
    Once again thanks

  11. 11 Ulf Zibis

    Peter Andersen’s static imports are for JUnit 3.x library.

    Here we are in JUnit 4.x library, so we have to use:
    import static org.junit.Assert.*;

    I’m missing god example for parametrized tests.
    - can you add some ?
    - Do you know a good link ?

    -Ulf

  12. 12 PeterK

    Anyone here tried TestNG out? It extends on the idea of annotations and also provides a larger set of execution control annotations. It’s JUnit style so the learning curve should be small.
    Ulf:
    TestNG also supports parameterization.

  13. 13 Abdullah Cetin CAVDAR

    I heard lots of good comments about TestNG but I haven’t tried it yet unfortunately. If anyone tried it, we would be happy to read his/her thougths. :D

  14. 14 Phani

    When I am running with @Ignore tag, JUnit is not ignoring that test case.
    Please can you check back once again?

  15. 15 Abdullah Cetin CAVDAR

    I checked it once again Phani. It seems, @ignore is working as stated in the post. The ignored test is shown in the test case list, however it is not executed actually.

  16. 16 Steven M. Armstrong

    Great summary.
    Your site has been recommended at my workplace for those migrating from jUnit 3 to jUnit 4.

    Thanks for the good work!

  1. 1 JUnit 4 en 60 secondes | Le blog de David Loureiro
  2. 2 JUnit 4 em 60 Segundos « André Faria Gomes

Leave a Reply




View Abdullah Çetin ÇAVDAR's profile on LinkedIn

Join My Community at MyBloglog!

Add to Technorati Favorites

Subscribe To My FriendFeed!

My Facebook Profile

E-Mail Subscriptions

Enter your email address:

My Flickr Photos

www.flickr.com

My Recent Readers