Skip to content

rweisleder/archunit-spring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ArchUnit Spring Integration

The ArchUnit Spring Integration provides predefined ArchUnit rules for Spring applications.

badge javadoc archunit spring

Usage

<dependency>
    <groupId>de.rweisleder</groupId>
    <artifactId>archunit-spring</artifactId>
    <version>1.2.0</version>
    <scope>test</scope>
</dependency>

The library expects that the required dependencies for Spring and ArchUnit are already declared.

Examples

Given the following class:

@RestController
class HelloRestController {

    @GetMapping("/hello")
    String hello() {
        return "Hello World";
    }
}

You can write architecture rules like this:

import static de.rweisleder.archunit.spring.SpringAnnotationPredicates.springAnnotatedWith;

methods()
    // "that are annotated with @RequestMapping" (or @GetMapping, @PostMapping etc.)
    .that(are(springAnnotatedWith(RequestMapping.class)))
    // "should be declared in classes that are annotated with @Controller" (or @RestController etc.)
    .should().beDeclaredInClassesThat(are(springAnnotatedWith(Controller.class)));

Unlike other rule libraries focused on architecture, this one encourages Spring-specific best practices and helps detect subtle bugs and antipatterns.

How about this?

@RestController("/hello")
class HelloRestController {

    @GetMapping
    String hello() {
        return "Hello World";
    }
}

Noticed the issue? A request to GET /hello would return HTTP 404.

Spring does not use the bean name in @RestController("…​") as a path mapping. The developer probably intended to write:

@RestController
@RequestMapping("/hello")
class HelloRestController {
    // ...
}

With the predefined rule SpringControllerRules.ControllerNameWithoutRequestMapping we’ve got you covered.

import de.rweisleder.archunit.spring.framework.SpringControllerRules;

@ArchTest
ArchRule ControllerNameWithoutRequestMapping = SpringControllerRules.ControllerNameWithoutRequestMapping;

User Guide

Please refer to the complete documentation here for detailed information.

About

Library providing predefined ArchUnit rules for Spring applications

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages