Hi, I’m trying to use konsist to enforce a “moduli...
# konsist
d
Hi, I’m trying to use konsist to enforce a “modulith” architecture. I have separate “subprojects” (not java modules) in a gradle project and I want to ensure that they don’t import each other and only communicate via an event bus using “common” type definitions (defined in an api project that they all import). Each module has it’s own package name (com.example.module1, com.example.module2, etc). What is the best way to express that with an architecture test?
I figured it out, here’s my test for anyone else who is interested:
Copy code
@Test
    fun `architecture is enforced`() {
        val modules = Konsist.scopeFromServices()
            .files
            .map { it.moduleName }
            //There is a common subproject that they can import
            .filter { !it.contains("/common/") }
            .toSet()

        Konsist
            .scopeFromServices()
            .assertArchitecture {
                val layers = modules.map { module ->
                    val packageName = 
module.replaceFirst(Regex(".*/"), "")
                    Layer(packageName, "com.example.services.$packageName..")
                }
                layers.forEach {
                    it.dependsOnNothing() //Nothing being the other layers defined here
                }
            }
    }
This does require that all packages in each module match the module name, but I can enforce that with another test. (scopeFromServices() is an extension function that just imports the scope from the path)
i
Nice. BTW We have plans to improve architectural checks soon. If you have any feedback then it's good moment to share it with us