Junit test case example in java eclipse

What you will learn here about Junit

  • JUnit test cases example in Eclipse

JUnit test cases example in Eclipse

Please follow the following steps to know how to write Junit test case in Eclipse
1)First create a maven project

2)Add maven dependency of junit in POM.xml which is shown below

  <dependency>
	  <groupId>junit</groupId>
	  <artifactId>junit</artifactId>
	  <version>4.13.2</version>
  </dependency>

Junit test case example in java eclipse

3)Create a class whose test case you want to write which is shown below

public class Person{

	String firstName;
	String lastName;
	public static void main(String[] args) {
		Person person=new Person();
		person.setFirstName(null);
		boolean  b=person.isNull(person.getFirstName());
		System.out.println(b);
		
	}
	
	public boolean isNull(Object object)
	{
		return object!=null;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
}



4)Now please follow the follow steps to create Junit test case

  1. Right click on class whose test case you want to create
  2. click on New
  3. Click on Junit Test Case

Junit test case example in java eclipse

5)After clicking on Junit Test Case please click on Finish which is shown below

6)After clicking on Finish Please delete the default method which is shown below

7)Now write your own method for testing methods and add @Test annotation on the top of it which is shown below

import org.junit.Assert;
import org.junit.Test;

public class PersonTest {

	@Test
	public void isNulltest() {
		Person person=new Person();
		person.setFirstName("joker");
		Assert.assertEquals(true, person.isNull(person.getFirstName()));
		
	}

}

8)Please click on Run to run the test case

How do you write JUnit test cases in Eclipse?

Creating a JUnit Test Case in Eclipse In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case. Make sure that the option at the top is set to use JUnit 4, not JUnit 3.

How do you write a JUnit case in Java?

TestJunitTestCaseExample.java.
package JavaTpoint.JunitExamples;.
import static org.junit.Assert.assertEquals;.
import org.junit.Test;.
public class TestJunitTestCaseExample {.
JunitTestCaseExample obj = new JunitTestCaseExample();.
@Test..
public void testAdd() {.
obj.add("Emma");.

How do I run a JUnit test case?

Create Test Runner Class It imports the JUnitCore class and uses the runClasses() method that takes the test class name as its parameter. Compile the Test case and Test Runner classes using javac. Now run the Test Runner, which will run the test case defined in the provided Test Case class. Verify the output.

What are JUnit test cases in Java?

The unit test case is a code which ensures that the program logic works as expected. The org. junit package contains many interfaces and classes for junit testing such as Assert, Test, Before, After etc.