blogs

How To Use Ignore Annotation In JUnit?

How To Use Ignore Annotation In JUnit? 05 Nov,2018

JUnit is an open source framework for Java programming language which is used for writing and running tests. This practice helps developers to discover failures in their logic behind the code and improve the quality of their code. Also, JUnit testing can be used in order to guarantee that, the code will work as expected in case of future changes. We can integrate JUnit Framework with Eclipse, Ant and Maven.

Do you know about JUnit annotations and their use?

Annotation is a predefined logic that is added java source code. Classes, methods, variables, parameters and packages may be annotated. JUnit uses annotations and works according to the annotation’s predefined logic.

JUnit framework works on following these annotations which are named as below:

1. @BeforeClass: This annotation will run once before any of the test methods in the class. This annotation neither provides any parameter nor returns any parameter. The @BeforeClass methods of superclasses will be run before those the current class.

2. @AfterClass: This annotation will run once after all the tests in the class have been run. All @AfterClass methods are guaranteed to run even if a BeforeClass method throws an exception. The @AfterClass methods declared in superclasses will be run after those of the current class. This annotation neither provides any parameter nor returns any parameter.

3. @Before: This annotation will run before each @Test. The @Before methods of super classes will be run before those of the current class. This annotation neither provides any parameter nor returns any parameter.

4. @After: This annotation will run after each @Test. The @After methods declared in super classes will be run after those of the current class. This annotation neither provides any parameter nor returns any parameter.

5. @Test: The Test annotation tells JUnit that this is the test that needs to be executed. Any exceptions thrown by the @Test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded. This annotation uses optional parameters as expected, timeout and so on.

6. @Ignore: Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests.

7. @RunWith: When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.

Have you ever faced problem with @Ignore annotation?

If you are not using JUnit and you want to temporarily disable a particular @Test which is partially implemented OR don’t want that @Test to fail while running a test methods, then you will be either comment that particular @Test or you will delete the @Test and the test runner class also not includes such @Test in report.

Solution:

JUnit provides you an annotation to overcome this problem named as @Ignore. The @Ignore annotation can be used when you want temporarily disable the execution of a particular @test. Every method that is associated with @Ignore won’t be executed. Test runner class also includes such @Test in report.

@Ignore annotation can be used in two ways:

1. To ignore particular @Test

2. To ignore a Class

Use @Ignore annotation with test method

A class could have numbers of test methods and some of them might not be required in execution due to some reasons like partial implemented and so on, then we use @Ignore annotation to skip them.

Below is the example that a class has two @Test methods, one is with @Ignore annotation and second @Test is without @Ignore annotation.

After execution the JUnit result will look like this as displayed in below screenshot:

Use @Ignore annotation with Class

@Ignore can also be used with Class to skip the whole class means all the @Test methods inside a class will be skipped.

Below is the example, how to ignore all the tests in class:

After execution the JUnit result will look like this as displaying in below screenshot:

You can also pass string argument in @Ignore annotation. This is an optional parameter, using this string parameter you can show the reason why you have ignored the @Test or Class.

Below is the example, how to pass string parameter in @Ignore annotation:

But output will be same as above, the parameter string used in @Ignore annotation is not printed in JUnit output.

I do hope that this post helps you in understanding the core concept of JUnit. Let me know your thoughts on this via comments and I will get back to you as soon as possible. Adios for now

Harinder Kaur Harinder works as Automation Test Engineer at BugRaptors. She is experts in writing scripts in web driver/Behat using Cucumber, gherkin languages. She has tested the web application on multiple Browsers and OS using Automation.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name field is required !

Related Posts

What Role Does Predictive Analytics Play In Software Testing...
How To Use Ignore Annotation In JUnit?

Ever wanted to predict the possible problems in your project even before their occurrence? Want to know what you should do to optimize your process? ...

Read More
Test Automation – Boosting Industrial Growth with Innovati...
How To Use Ignore Annotation In JUnit?

Automation Market summary   Test automation software’s are totally serving to a big selection of sectors in optimizing software test take a...

Read More
How Artificial Intelligence(AI) Will Change Software Testing...
How To Use Ignore Annotation In JUnit?

Software testing is an important phase in making the product reliable and satisfactory for the customers. Since customers, satisfaction is the main fo...

Read More
Cucumber: Best BDD Tool For Automation
How To Use Ignore Annotation In JUnit?

Based on Behavior Driven Development (BDD) framework, Cucumber is an automation tool which is used to write tests for a web application. Using cucumbe...

Read More

Popular Blogs

What Role Does Predictive Analytics Play In Software Testing?
What Role Does Predictive Analytics Play In Software Testing? Read More
How Industry 4.0 Can Harness The Power of Quality Assurance?
How Industry 4.0 Can Harness The Power of Quality Assurance? Read More
How To Achieve Great UX With The Help of QA?
How To Achieve Great UX With The Help of QA? Read More
Advantages To Outsource QA Software Testing
Advantages To Outsource QA Software Testing Read More
Test Automation – Boosting Industrial Growth with Innovation
Test Automation – Boosting Industrial Growth with Innovation Read More

Sign up FOR NEWSLETTER !

Consult our Experienced Team of Testers for Your QA Requirements!
contact us