How to Merge Multiple Annotations in JAVA

Hello all, in this article I will demonstrate you how to use a common custom annotation to merge the other annotations in JAVA. If you need to use multiple annotations in your test classes, you can use this technique to merge some annotations. In this way, whenever you need to add or change some annotation for your test classes, you can easy to modify it in your base annotation.

In order to do this, you need to create a custom annotation class. Let’s name it BaseTestAnnotation and lets add @RepeatedIfExceptionsTest(repeats = 3) annotation in this class. In this way, we can make this annotation as global to all of our test classes.

After that, we can use BaseTestAnnotation class instead of @Test annotation as shown below.

and when we run our test, we will see that our tests will repeat 3 times when they fail.

If you want to change retry count in your tests, you can just change the retry count in BaseTestAnnotation file. If you don’t use this technique and if you put @RepeatedIfExceptionsTest(repeats = 3) annotation on top of your all test methods, then you need to change the repeat count for each test method. It will be a cumbersome process for us. But with common base test annotation, it will be so easy to change repeat count for all test methods.

Note: New JUnit 5 parallel execution feature is not compatible with Repeat feature. If you try to run your tests in parallel, they will run in parallel but they won’t rerun when they will fail. You can see this on below screenshot. Here is the comprehensive JUnit 5 Repeat Test article.

Thanks.
Onur Baskirt

By |2018-11-28T01:06:28+00:00October 28th, 2018|Java Tutorials|0 Comments

About the Author:

Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as a research assistant, design verification engineer, and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. After Teknosa, he worked as Head of Software Testing and Manager of two Development Teams at Kariyer.net. Now, he is working as a Senior Technical Consultant at Emirates Airlines in Dubai. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.