TNG / ArchUnit

A Java architecture test library, to specify and assert architecture rules in plain Java
http://archunit.org
Apache License 2.0
3.18k stars 288 forks source link

Add entry point `onlyClasses` and similar #1196

Open rweisleder opened 10 months ago

rweisleder commented 10 months ago

Because of a recent discussion, I extract my suggestion from this comment into a separate issue.

It would be great to have onlyClasses() as entry point. Rules created with onlyClasses() should fail

  1. if classes that match the that condition, don't match the should condition, and
  2. if classes that don't match the that condition, match the should condition.

Same goes for onlyMethods(), onlyFields(), only(ClassesTransformer) etc.

The current alternative is to write rules "twice":

CompositeArchRule.of(
    classes().that().areAnnotatedWith(RestController.class).should().haveSimpleNameEndingWith("RestController")
).and(
    classes().that().haveSimpleNameEndingWith("RestController").should().beAnnotatedWith(RestController.class)
).as("only classes that are annotated with @RestController should have simple name ending with 'RestController'")

// vs.

onlyClasses().that().areAnnotatedWith(RestController.class).should().haveSimpleNameEndingWith("RestController")