0

我正在尝试使用 Arch Unit 设置测试来测试我的命名约定和注释。

我有这堂课:

@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)
    }
}

我的班级测试是这个:

@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);
}

但我得到这些错误:

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)

我不知道如何告诉拱门忽略同伴,何时或其他不是我的课。

我究竟做错了什么?

4

1 回答 1

0

也许您想将约定限制在顶级课程

    ArchRuleDefinition.classes()
            .that().resideInAPackage("..controller")
            .and().areTopLevelClasses()
            .should().haveSimpleNameEndingWith("Controller")
            .andShould().beAnnotatedWith(RestController.class);
于 2021-12-15T23:45:47.760 回答