JUnit test cases example in 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 maven dependency

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;
	}
	
}

java class

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

Eclipse create junit test case

5)After clicking on Junit Test Case please click on Finish which is shown below
Junit test case in Eclipse

6)After clicking on Finish Please delete the default method which is shown below
how to write Junit test case in Eclipse

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
JUnit test cases example in Eclipse

You may also like...