我正在嘗試使用 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)
}
}
我的班級 Test 是這樣的:
@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)
我不知道如何告訴 Arch 單位忽略同伴,何時或其他不是我的課程。
我究竟做錯了什么?
uj5u.com熱心網友回復:
也許您想將您的約定限制為頂級類?
ArchRuleDefinition.classes()
.that().resideInAPackage("..controller")
.and().areTopLevelClasses()
.should().haveSimpleNameEndingWith("Controller")
.andShould().beAnnotatedWith(RestController.class);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383396.html
