<dependency>
<groupId>de.rweisleder</groupId>
<artifactId>archunit-spring</artifactId>
<version>0.2.0</version>
<scope>test</scope>
</dependency>
The library expects that the required dependencies for Spring and ArchUnit are already declared.
Given the following class:
@RestController
class DemoRestController {
@GetMapping("/hello")
String hello() {
return "Hello World";
}
}
You can write architecture rules like this:
import static de.rweisleder.archunit.spring.MergedAnnotationPredicates.springAnnotatedWith;
// "methods that are annotated with @RequestMapping (or @GetMapping, @PostMapping etc.)
// should be declared in classes that are annotated with @Controller (or @RestController etc.)"
methods()
.that(are(
springAnnotatedWith(RequestMapping.class)
.as("annotated with @RequestMapping (or @GetMapping, @PostMapping etc.)"))
)
.should().beDeclaredInClassesThat(are(
springAnnotatedWith(Controller.class)
.as("annotated with @Controller (or @RestController etc.)")
));
// "methods that are annotated with @RequestMapping (or @GetMapping, @PostMapping etc.)
// should have path containing only lowercase characters"
methods()
.that(
are(springAnnotatedWith(RequestMapping.class)
.as("annotated with @RequestMapping (or @GetMapping, @PostMapping etc.)"))
)
.should(
be(springAnnotatedWith(RequestMapping.class,
describe("@RequestMapping(path=<lower-case>)",
requestMapping -> Arrays.stream(requestMapping.path()).allMatch(path -> path.toLowerCase().equals(path))))
).as("have path containing only lowercase characters")
);
For each release of this project, compatibility is confirmed with the most recent version of ArchUnit and the minor versions of Spring projects with OSS support available at that release’s time.
ArchUnit Spring version | Confirmed Compatibility | |
---|---|---|
ArchUnit |
0.2.0 |
1.2.1 |
Spring Boot |
0.2.0 |
3.1, 3.2 |
Spring Framework |
0.2.0 |
5.3, 6.0, 6.1 |
Compatibility with other versions is possible, but not guaranteed.
For business support, including assistance with making the library compatible with unlisted versions or creating integrations with other Spring projects, please contact me directly.