Home > database >  Kotlin ArchUnit : How to set rules doesn't ask for object that belongs to kotlin language?
Kotlin ArchUnit : How to set rules doesn't ask for object that belongs to kotlin language?

Time:12-16

I'm trying to setup tests with Arch Unit to test my naming conventions and annotations.

I have this class:

@RestController
@RequestMapping("/uri")
class AnyController() : SomeResources {

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    override fun create(
        @Valid @RequestBody requestDTO: requestDTO,
        ): ResponseEntity<ResponseDTO> {

        when (algo) {
            algo -> logger.debug("[algo: ${algo}]")
            else -> logger.debug("[algo: ${algo}]")
        }
        
        return ResponseEntity.status(HttpStatus.CREATED).body(service.create(requestDTO))
    }

    companion object {
        private val logger: Logger = LoggerFactory.getLogger(AnyController::class.java)
    }
}

And my class Test is this one:

@AnalyzeClasses(packages = "some.package",
        importOptions = {
                ImportOption.DoNotIncludeTests.class,
                ImportOption.DoNotIncludeJars.class,
                ImportOption.DoNotIncludeArchives.class
        })
class ArchitectureTest {

    @ArchTest
    public static final ArchRule controllers_name_classes_should_finish_with_Controller = ArchRuleDefinition.classes().that().resideInAPackage("..controller").should().haveSimpleNameEndingWith("Controller");

    @ArchTest
    public static final ArchRule controllers_classes_should_use_annotation_RestController = ArchRuleDefinition.classes().that().resideInAPackage("..controller").should().beAnnotatedWith(RestController.class);
}

But I get these errors:

java.lang.AssertionError: Architecture Violation [Priority: MEDIUM] - Rule 'classes that reside in a package '..controller' should be annotated with @RestController' was violated (2 times):
Class <some.package.controller.AnyController$WhenMappings> is not annotated with @RestController in (AnyController.kt:0)
Class <some.package.controller.AnyController$Companion> is not annotated with @RestController in (AnyController.kt:0)


java.lang.AssertionError: Architecture Violation [Priority: MEDIUM] - Rule 'classes that reside in a package '..controller' should have simple name ending with 'Controller'' was violated (2 times):
simple name of some.package.controller.AnyController$Companion does not end with 'Controller' in (AnyController.kt:0)
simple name of some.package.controller.AnyController$WhenMappings does not end with 'Controller' in (AnyController.kt:0)

I don't know how to tell arch unit ignore companion, when or something else that is not my class.

What am I doing wrong?

CodePudding user response:

Maybe you want to restrict your conventions to top-level classes?

    ArchRuleDefinition.classes()
            .that().resideInAPackage("..controller")
            .and().areTopLevelClasses()
            .should().haveSimpleNameEndingWith("Controller")
            .andShould().beAnnotatedWith(RestController.class);
  • Related