Home » Core Java » junit » JUnit Disable Test Example

About Alvin Reyes

Alvin Reyes
Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.

JUnit Disable Test Example

1. Introduction

There are instances that our test cases aren’t ready yet and it would almost be certain that when we run our builds with these, there is a high probability that our test executions will fail. This can be avoided using the @Ignore annotation.

Developers sometimes just pass the test case to avoid build failures (assert(true)). This is a bad practice and should be avoided. The @Ignore annotation is a better alternative as it will allow the runner to just ignore the test case ensuring that it won’t be part of the build run.

This annotation can be used by developers to tag a specific test case as disabled. This means that even if we run our builds, the JUnit test runner won’t bother running them since it assumes that it’s not yet ready or intentionally disabled.

2. Source

Let’s start with creating an implementation class. The following code will be our implementation class that we will create a test case from.

MessageService.java

package com.areyes.junit.svc;

/**
 * The Class MessageService.
 */
public class MessageService {
	
	

	/** The message. */
	private String message;


	/**
	 * Instantiates a new message service.
	 *
	 * @param message the message
	 */
	public MessageService(String message) {
		this.message = message;
	}

	/**
	 * Prints the message.
	 *
	 * @return the string
	 */
	public String printMessage() {
		return message;
	}

	/**
	 * Salutation message.
	 *
	 * @return the string
	 */
	public String salutationMessage() {
		message = "Hi!" + message;
		return message;
	}
	
	/**
	 * This will be the method to get the salutation messages specifically for executives.
	 * @return
	 */
	public String salutationMessageForExecutives() {
		return "this is not yet implemented";
	}

}

As it can be seen on the class above, we have a method that has not been implemented yet. We don’t want our test case to run this don’t we? So in order for our test case to ignore this, we use the @Ignore on the specific test. See the test case below.

MessageServiceTest.java

package com.areyes.junit.svc;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.isA;

public class MessageServiceTest {

	private String CONST_MSG = "Message is me";
	private MessageService msgService = new MessageService(CONST_MSG);
	
	@Test
	public void testPrintMessage() {
		//	Check type return
		assertThat(msgService.printMessage(), isA(String.class));
		assertEquals(CONST_MSG, msgService.printMessage());
		
	}
	
	@Test
	public void testSalutationMessage() {
		String messageSal = msgService.salutationMessage();
		assertThat(messageSal, isA(String.class));
		assertEquals("Hi!" + CONST_MSG,messageSal);
	}


	@Ignore
	@Test
	public void testSalutationMessageForExecutives() {
		assertThat(msgService.salutationMessageForExecutives(), isA(String.class));
		assertEquals(CONST_MSG, msgService.salutationMessage());
	}
}

As seen on the testSalutationMessageForExecutives() method, it’s as simple as putting an@Ignoreannotation on your test case. This is our way (developers) to tell the JUnit Runner that we don’t want to run this specific case since we haven’t finished the implementation yet.

3. Result

Result of running the test in Eclipse

Figure 1.0 JUnit Disable / Ignore example

Figure 1.0 JUnit Disable / Ignore example

4. Download the Eclipse project

This was an example of JUnit Disable Test

Download
You can download the full source code of this example here: junit-disable-example
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of