Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

java - ArchUnit doesnt seem to cache analyzed classes

I wrote some ArchUnit-Tests using Jupiter. I found examples that indicated, you can write your ArchUnit tests using non static methods, like:

@Test
void enforceExceptionNames() {
    classes().that()
             .areAssignableTo(Exception.class)
             .and(modifier(PUBLIC))
             .and().areNotAnnotatedWith("some.qa.ExceptionNameFlaw")
             .should()
             .haveNameMatching(".*Exception").orShould()
             .haveNameMatching(".*Error")
             .check(modulClasses);
}

The advantage is, that you can do things that arent possible static - like extracting the packagename using reflection at runtime and other stuff.

But performance is poor. Like 1-10 seconds depending on size.

Eitherway ArchUnit states to have all Classes cached static.

question from:https://stackoverflow.com/questions/65951425/archunit-doesnt-seem-to-cache-analyzed-classes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It semms caching ONLY occurs when using @AnalyzeClasses and @ArchTest annotated static fields or methods.

After transforming my tests to this pattern, performance skyrockets to 0.05 seconds!

@AnalyzeClasses(packages = "some.svc.gui.impl")
public class StandardCodeModuleTest extends StandardCodeTest {

    @ArchTest
    public static final ArchRule ENFORCE_EXCEPTION_NAMES = classes()
            .that()
            .areAssignableTo(Exception.class)
            .and(modifier(PUBLIC))
            .and().areNotAnnotatedWith("some.qa.ExceptionNameFlaw")
            .should()
            .haveNameMatching(".*Exception").orShould()
            .haveNameMatching(".*Error");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...