Git Product home page Git Product logo

Comments (7)

codecholeric avatar codecholeric commented on June 23, 2024 4

Roaster looks neat, I might play around with that myself a little 😉 Might cover the source aspect that is missing from ArchUnit, for static code analysis.
For your use case, you should be fine, to do sth. like this:

@Test
public void only_use_Hamcrest() {
    JavaClasses classes = new ClassFileImporter().importPackages("com.myapp");

    ArchRule rule = noClasses()
            .should().accessClassesThat()
                .haveFullyQualifiedName(org.assertj.core.api.Assertions.class.getName())
            .orShould().accessClassesThat()
                .resideInAPackage("org.testng")
            .because("we consistently want to use Hamcrest in our tests");

    rule.check(classes);
}

(There is no specific TestNG support for ArchUnit up to now, like the ArchUnitRunner for JUnit 4)

from archunit.

codecholeric avatar codecholeric commented on June 23, 2024

Hi Georgy, I think what you want is possible, but there might be several ways, so it depends a little on your use case. First of all, ArchUnit has no concept of imports, since it only analyzes bytecode.
However, I think you could achieve what you want either by naming:

@ArchTest
public static final ArchRule rule_via_package = noClasses().that().haveNameNotMatching(".*Test")
        .should().accessClassesThat().resideInAPackage("org.junit..");

That works, if your external library has a distinguishing package. Or you could use the source of your Java classes, which is not integrated in the default fluent API, but can easily be added:

@ArchTest
public static final ArchRule rule_via_source = 
    noClasses().that(haveSourceMatching(".*/test/.*"))
        .should().accessClassesThat(haveSourceMatching(".*junit.*\\.jar.*"));

private static DescribedPredicate<JavaClass> haveSourceMatching(final String regex) {
    return new DescribedPredicate<JavaClass>("have source matching '%s'", regex) {
        @Override
        public boolean apply(JavaClass input) {
            return input.getSource().isPresent()
                    && input.getSource().get().getUri().toString().matches(regex);
        }
    };
}

Of course you can mix and match those approaches, like restricting the classes by name and the targets by source, etc.
Note that this only catches accesses though, so a dependency like an annotation is not captured. I'm working on improving this in the next version (you could add this yourself though, if you need it, I can also copy & paste some code here 😉)
But for test assertions it should work, since those are real accesses in bytecode, i.e. method calls, etc.
Does this help you?

from archunit.

gryabov avatar gryabov commented on June 23, 2024

Peter, I'll try your solution. Thank you.

My case is the next:

One of the rule.
I write tests using testng and three assertion libs are accessible from the tests.
I want to use only Hamcrest assertions, but not testng or assertj.

This is not actually architecture rules but with this, I'd like to help novices in the team and rid of code mess.

from archunit.

gryabov avatar gryabov commented on June 23, 2024

For code source approach I look towards https://github.com/forge/roaster
I already use it to extract useful statistic from my test.

from archunit.

gryabov avatar gryabov commented on June 23, 2024

Peter, thank you for help and ArchUnit.

from archunit.

codecholeric avatar codecholeric commented on June 23, 2024

No probs 😄 If that solved your problem, is it okay to close the issue?

from archunit.

gryabov avatar gryabov commented on June 23, 2024

Yeah, that is solved.

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.