How to Skip or Ignore JUnit test cases in Java


At times you may not want to run certain test cases in Junit, in such cases make use of the annotation @Ignore on such test methods.

Greeting.java
package org.code2care;

public class Greeting {

	public String wishMorning(String name) {
		return "Hello " + name + ", Good Morning!";
	}
	
	public String wishAfternoon(String name) {
		return "Hello " + name + ", Good Afternoon!";
	}
	
	public String wishEvening(String name) {
		return "Hello " + name + ", Good Evening!";
	}


}
GreetingTest.java
package myprog;

import static org.junit.Assert.assertEquals;

import org.code2care.Greeting;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class GreetingTest {


	Greeting greeting;
	
	@Before
	public void init() {
		greeting = new Greeting();
	}
	
	
	@Test
	@Ignore
	public void wishMorningTest() {
		assertEquals("Hello Neo, Good Morning!", greeting.wishMorning("Neo"));
	}
	
	@Test
	public void wishAfternoonTest() {
		assertEquals("Hello Neo, Good Afternoon!", greeting.wishAfternoon("Neo"));
	}
	
	@Test
	public void wishEveningTest() {
		assertEquals("Hello Neo, Good Evening!", greeting.wishEvening("Neo"));
	}
	
	

}
Test Run Results:
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running myprog.GreetingTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.012 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 1
Ignore or Skip Junit Test Cases

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap