Git Product home page Git Product logo

Comments (2)

codecholeric avatar codecholeric commented on June 23, 2024 1

Resolved by #128

from archunit.

codecholeric avatar codecholeric commented on June 23, 2024

I take it, you're not using the JUnit 4 support, are you? Because then you could simply aggregate Tests like

public class MyAggregatedRules {
    @ArchTest
    public static final ArchRules rules1 = ArchRules.in(FirstRuleSuite.class);

    @ArchTest
    public static final ArchRules rules2 = ArchRules.in(SecondRuleSuite.class);
}

(which works recursively, this is how we compose SpecificRules -> LessSpecificRules -> AllRules -> ConcreteTest)

This will work with the JUnit 5 support as well, once it's finished.
But if you're writing your tests as plain unit tests, ArchRules is not usable in a comfortable way, you're right about that. You can add something like this yourself in the following way:

public class RuleSuite implements ArchRule {
    private final List<ArchRule> rules;
    private String overriddenDescription;

    public RuleSuite(ArchRule... rules) {
        checkArgument(rules.length > 0, "A suite of 0 rules makes no sense");
        this.rules = Arrays.asList(rules);
    }

    @Override
    public void check(JavaClasses classes) {
        Assertions.check(this, classes);
    }

    @Override
    public ArchRule because(String reason) {
        rules.get(rules.size() - 1).because(reason);
        return this;
    }

    @Override
    public EvaluationResult evaluate(JavaClasses classes) {
        EvaluationResult result = new EvaluationResult(this, Priority.MEDIUM);
        for (ArchRule rule : rules) {
            result.add(rule.evaluate(classes));
        }
        return result;
    }

    @Override
    public String getDescription() {
        if (overriddenDescription != null) {
            return overriddenDescription;
        }

        List<String> descriptions = new ArrayList<>();
        for (ArchRule rule : rules) {
            descriptions.add(rule.getDescription());
        }
        return Joiner.on(lineSeparator() + " and ").join(descriptions);
    }

    @Override
    public ArchRule as(String newDescription) {
        overriddenDescription = newDescription;
        return this;
    }
}

This will join all rule descriptions and after that join all errors of all rules in one shot, instead of failing after the first.

I have pondered about making ArchRules usable in plain unit tests in a pretty similar fashion, but I have to see what the requirements of the JUnit test support are, once I'm done with the JUnit 5 support (since it was originally composed for that).

I think you're right though, that ArchUnit should offer a way out of the box to create such composite rules, even it no extra testing support is used. I'll think about it, where this could be added the best (whether it's ArchRules or another composite similar to the one above).

I hope you can use that POC for now though for your use case, let me know if you have trouble!!

from archunit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.