JUnit Tutorial

junit-logo

JUnit, a popular unit test framework in Java. In this tutorials, all examples are tested with JUnit 4.12

1. JUnit 4.x Examples

References

  1. JUnit Official site
  2. Wikipedia – JUnit

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

28 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Sky
15 years ago

Thank you!

Ajay
6 years ago

sir, i installed junit 5 it shows an error. eclipse also shows an error . Java jre and java se is not installing only document is there. one website shows Java SE. Which software will work in Windows. Linux and macintosh are not installing in Windows. is there any software with no errors.

paramesh
9 years ago

Please anyone give me the suggestion as soon as possible bcaz I am stucking in that long time

paramesh
9 years ago

I want to ensure whether my method successfully executed or not in Junit. Is
there any way to test?

asrabanu bagvan
10 years ago

Hi yong,
Can u please provide me the guide for deploying enterprise applications in ibm websphere application server 8.5.
And how to create profile,http port and etc

Jefferson_P_Thomas
12 years ago

Junit Tutorials Series On Techno Nutty Junit Tutorials Series

Anant
12 years ago

Hi, can you please explain all the methods like as setUp() & tearDown(),
means what are the significance of these method and how to be use using spring?

Mohit
12 years ago

For more tutorials on features like Categories, test suites, hamcrest and recommended junit books, read : JUnit tutorials

Praveen
8 years ago
Reply to  Mohit

It is showing database connection error “Error establishing a database connection”

P singh
12 years ago

can you tell me how to embed multiple test case in one file
i means can we write two test() function in one file to test two different function.
thanks

P singh
12 years ago

can you tell me how to embed multi test case in one file
i means can we write two test() function in one file to test two different function.
thanks

sruthi
13 years ago

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

Sumesh
13 years ago

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/

nityananda
13 years ago

please provide tapestry tutorial…..soon.

Ravi Kant Soni
13 years ago

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…..

bijay kumar
13 years ago

hi mkyong,
plz provide Tapestry & Adobe Flex tutorial….

bijay kumar
13 years ago

hi mkyong,
plz provide Tapestry tutorial….

srinivas
13 years ago

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

 package concurrent;
import java.net.URL;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junitext.XMLParameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.junitext.runners.XMLParameterizedRunner;
@RunWith(XMLParameterizedRunner.class)
public class One_inTwo {
  public String browser1;
  @XMLParameters("/concurrent/One_inTwo1.xml")
  public One_inTwo(String browser1) {
     this.browser1=browser1;    
  }
  @Test 
  public void test() throws Exception {
    WebDriver driver;
    URL server = new URL("http://localhost:8081/wd/hub");
    DesiredCapabilities capabilities = null;
		driver = new RemoteWebDriver(server, Common.getGridsettings(browser1, capabilities));
		driver.get("http://google.com");
		Thread.sleep(3000);
		WebElement search_editbox   =   driver.findElement(By.name("q"));
		WebElement search_button    =   driver.findElement(By.name("btnG"));
		search_editbox.clear();
		search_editbox.sendKeys("first");
		search_button.click();
	driver.quit();

  }
}


and the xml code is

<?xml version="1.0" encoding="UTF-8" ?>
<tests>
  <test>
     <string id="browser1" value="firefox" />
  </test>
  <test>
     <string id="browser1" value="IE" />
  </test>
</tests>
srini
13 years ago

how to make the immutable class ? give me example?

Githin
13 years ago
Reply to  srini

Make class Final.

make data and methods too final and static.

Raman Kumar
13 years ago
Reply to  Githin

yes… use the final keyword before class name…

arthy
13 years ago

Hi Yong, Could you provide some pointers for adding methods(not class level) to a test suite in JUNIT4..?

basha
14 years ago

how can write test cases for the private method using junit test3.2

Angel
13 years ago
Reply to  basha

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:)

test
15 years ago

nice

my Test
12 years ago

Minimalist and useful
Thank you

Sujeesh
13 years ago

These tutorials are really helpful. Thank you Mkyong.