JUnit Tutorial

JUnit is the most famous unit test framework in Java. However it is only suitable for pure unit testing, for integration test, you should go for TestNG instead.
Happy learn JUnit :)
JUnit 4.x Tutorials
- Tutorial 1 – Basic usage
Introduces basic annotation supported in Junit 4. - Tutorial 2 – Expected Exception Test
Test the exception throw by the method. - Tutorial 3 – Ignore Test
Ignore certain method for unit test. - Tutorial 4 – Time Test
Make sure the testing method will return after certain time. - Tutorial 5 – Suite Test
Bundle a few unit test cases and run it together. - Tutorial 6 – Parameterized Test
How do pass parameter value for unit test.
JUnit Vs TestNG
- JUnit 4 Vs TestNG – Comparison
Detail feature comparison between JUnit 4 and TestNG.

i need to display message in jsp from servlets.but im getting null value after loading jsp page..can i please get the solution for this
This tutorial is really nice. But if you want to overcome some of the major limitations of junit 3 and junit 4 have a look at the below mentioned link which tells how to use effectively use junit for functional testing.
http://apitestingwithjunit.blogspot.in/
These tutorials are really helpful. Thank you Mkyong.
please provide tapestry tutorial…..soon.
hi Monk, here is the here which i am getting while UNIT Test for “https” url.
Plz help me as soon as possible.
SSLUnsubscribes exception…..
hi mkyong,
plz provide Tapestry & Adobe Flex tutorial….
hi mkyong,
plz provide Tapestry tutorial….
hi can anybody help me on an issue with XMLParameterizedRunner in JUnit4.8
i am getting the following error
java.lang.NoSuchMethodError: org.junit.internal.runners.MethodValidator.(Ljava/lang/Class;)V
at org.junit.internal.runners.TestClassRunner.(TestClassRunner.java:26)
at org.junitext.runners.XMLParameterizedRunner.(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.(JUnit4TestReference.java:32)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
and the java code is
and the xml code is
how to make the immutable class ? give me example?
Make class Final.
make data and methods too final and static.
yes… use the final keyword before class name…
Hi Yong, Could you provide some pointers for adding methods(not class level) to a test suite in JUNIT4..?
how can write test cases for the private method using junit test3.2
Unit test unable to test private method.
It is possible to test private methods in your class by different ways. An alternative solution could be to use/implement Java Reflection API.Something like this :
Method theMethod = targetClass.getDeclaredMethod(methodName, argClasses);
theMethod.setAccessible(true);
return theMethod.invoke(targetObject, argObjects);
Or take a look at this http://code.google.com/p/powermock/wiki/BypassEncapsulation
good luck:)
Thank you!
nice