How to exclude a package from Archunit Tests?

Viewed 256

In my root project I have two sub projects with that package structure.

Project 1: com.app
Project 2: com.app.api

In Project 1 I have a Class definied with ArchRules, anotaed like that

@AnalyzeClasses( packages = "com.app")
public class ArchTests

The Problem is that if I run that test, its analyze everything from Project 2 too. How I can exclude package com.app.api?

1 Answers

You can use custom importOptions:

@AnalyzeClasses(packages = "com.app", importOptions = ExcludeApiImportOption.class)

where the ImportOption is bascially a predicate whether a Location should be imported or not:

class ExcludeApiImportOption implements com.tngtech.archunit.core.importer.ImportOption {
    @Override
    public boolean includes(Location location) {
        return  // ... whether location is not in "com.app.api"
    }
}
Related