Search code examples
spring-boot

Annotation for unit testing service layer in isolation


For testing controller in isolation we have @WebmvcTest available in spring boot test. It would only configure controller class and no dependencies for it. Similarly for jpa layer we have @DatajpaTest

Is there something similar for service beans?


Solution

  • Just unit test them, no need for spring context.

    DataJpa and SpringWeb are PARTS of spring therfore you have to boot it up to have reliable tests that includes all Spring behaviors into the equasion

    Service layer on the other hand, are just POJOs, so unit testing those are the easiest and most straightforward of all.

    PS. just dont use field injection but via constructor and/or via setters and you will be fine.