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

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.

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.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.