Kotlin ArchUnit:如何设置规则不要求属于 kotlin 语言的对象?
Kotlin ArchUnit : How to set rules doesn't ask for object that belongs to kotlin language?
我正在尝试使用 Arch Unit 设置测试来测试我的命名约定和注释。
我有这个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)
}
}
我的 class 测试是这个:
@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 单位忽略同伴,什么时候或其他不是我的 class。
我做错了什么?
也许您想将约定限制为 top-level classes?
ArchRuleDefinition.classes()
.that().resideInAPackage("..controller")
.and().areTopLevelClasses()
.should().haveSimpleNameEndingWith("Controller")
.andShould().beAnnotatedWith(RestController.class);
我正在尝试使用 Arch Unit 设置测试来测试我的命名约定和注释。
我有这个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)
}
}
我的 class 测试是这个:
@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 单位忽略同伴,什么时候或其他不是我的 class。
我做错了什么?
也许您想将约定限制为 top-level classes?
ArchRuleDefinition.classes()
.that().resideInAPackage("..controller")
.and().areTopLevelClasses()
.should().haveSimpleNameEndingWith("Controller")
.andShould().beAnnotatedWith(RestController.class);