I played with JUnit 4 library this weekend and here is the short introduction to it:
- @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. - @Before and @After
Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case. - @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. - 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. - @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.
- Timeout
Define a timeout period in miliseconds with “timeout” parameter. The test fails when the timeout period exceeds. - 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); - JUnit4Adapter
Run your Junit 4 tests in Junit 3 test runners with Junit4Adapter.
Happy coding.



















Nice one, thanks!
sometimes I’m not sure if I really exist
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
Thanks for reminding the static imports Peter.
westlakken, maybe you are just someone else’s reflection.
nice junit guide. appreciate!
Nice… is great for me, and very easy, thanks..
Great way to explain in less time
clear and concise explanation! thank you.
Nice tutorial
Hey appreciate yor help regarding this
I am moved from a novice user to some kind of a basic user
Once again thanks
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
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.
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.
When I am running with @Ignore tag, JUnit is not ignoring that test case.
Please can you check back once again?
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.
Great summary.
Your site has been recommended at my workplace for those migrating from jUnit 3 to jUnit 4.
Thanks for the good work!