Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to combine JUnit annotations into a custom annotation?

 
Ranch Hand
Posts: 72
1
Python Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A guy some time ago told me it's possible and while I really tried to RTFM and google, I couldn't get a clue.

The problem is that my unit tests each make use of a not-small JUnit annotation system, something like this:
@ParameterizedTest
@MethodSource("myorg.ccrtest.testlogic.DataProviders#standardDataProvider")
@Tags({@Tag("ccr"), @Tag("standard")})

This makes test authoring a little tedious, test code a little long and of course, when a change is needed, it's a chore!

Was wondering if I could create my own JUnit annotation: @CcrStandardTest, which would imply all of the tags above?

I also tried shifting the tag up in the class definition (hoping they would then apply to all methods of the class), but the compiler says no: "@ParameterizedTest is not applicable to type"
 
Todor Kolev
Ranch Hand
Posts: 72
1
Python Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I figured out something...
The key was to find how what I am looking for is called Once you know that, you can search for more info easily.

What I was looking for is "composed annotations", sometimes referred to as "meta annotations":
JUnit5 Docs - Composed Annotations

----

I created the following:



Then, I was able to tag my tests with @CcrStandardTest and derive all the above annotations in an elegant way.

Initially, I tried without @Retention and @Target - but then the composed annotation was not resolved - tests were either not discovered for execution,or not compiling due to "missing" method source.
IDE still managed to see through that and complained when I used both my composed annotation and @Test (while I was investigating) but IDE isn't important here.
- Not sure what these two tags do exactly (I have a guess but I am not brave enough to share), as the JUnit doc doesn't elaborate...



 
Todor Kolev
Ranch Hand
Posts: 72
1
Python Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More info about the two annotations mentioned in last paragraph:

@Retention
*********
Annotations in Java have three different retention policies:
1. RetentionPolicy.SOURCE
- Annotations are to be discarded by the compiler.
2. RetentionPolicy.CLASS
- Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior. [emphasis added]
3. RetentionPolicy.RUNTIME
- Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

@Target:
*******
Indicates the contexts in which an annotation type is applicable. The declaration contexts and type contexts in which an annotation type may be applicable are specified in JLS 9.6.4.1, and denoted in source code by enum constants of java.lang.annotation.ElementType.
If a @Target meta-annotation is not present on an annotation type T , then an annotation of type T may be written as a modifier for any declaration except a type parameter declaration.
If a @Target meta-annotation is present, the compiler will enforce the usage restrictions indicated by ElementType enum constants, in line with JLS 9.7.4.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I hope this info is useful to u.
JUnit Jupiter annotations can be used as meta-annotations. That means that you can define your own composed annotation that will automatically inherit the semantics of its meta-annotations.

For example:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@ParameterizedTest
@MethodSource("myorg.ccrtest.testlogic.DataProviders#standardDataProvider")
@Tag("ccr")
@Tag("standard")
public @interface CcrStandardTest {}
You would then place the composed annotation on your test method:

@CcrStandardTest
void testFoo(/* necessary parameters */) {
 // perform test
}
I have found this information in W3softech , which is best software testing company in India.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic