I want to write an archunit rule in my J2EE project to test when a class is annotated with @Startup, then it must contain a method that is annotated with @PostConstruct.
I tried this
@ArchTest
private val startUpBeansShouldHavePostConstruct: ArchRule = classes().that()
.areAnnotatedWith(Startup::class.java)
.shouldHave(methods().that().areAnnotatedWith(PostConstruct::class.java))
.because("classes with @Startup should contain a @PostConstruct annotated method")
but that doesn’t compile in the shouldHave()-line. I am a bit stuck how to define such a rule. My actual goal is to have a rule that says there should be exactly one annotated method.
New contributor
MichaelM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.