Is there anything like Lombok @AllArgConstructor for @MockBeans?

Viewed 118

I use Project Lombok for my Java projects.

My Controller looks like this:

@RestController
@AllArgsConstructor
public class UserController {

    private RegisterService registerService;
    private UserService userService;
    private UserValidator userValidator;
    private LoginService loginService;
    /*
     * rest of the controller
     */
}

so the controller must look like this:

@WebMvcTest(UserController.class)
public class UserControllerTest {

    @MockBean
    private UserRepository userRepository;

    @MockBean
    private RegisterService registerService;

    @MockBean 
    private UserService userService;

    @MockBean
    private LoginService loginService;
    
    @MockBean
    private UserValidator UserValidator;

    /*
     * rest of the contoller test
     */
}

Just to make programming less code-monkey, is there anything like @AllMockArgConstructor?
Or any way, I don't always have to add all services?

If this is a stupid question, please explain me why.

Thanks in advance!

1 Answers

Unfortunately it cannot be done,

@MockBeans annotation target is applied only to fields and types, and not for methods, you can read more about it here.


If @MockBeans was able to support also methods then it was can be done that way:

@Getter(onMethod_={@MockBean} )
@WebMvcTest(UserController.class)
public class UserControllerTest {
   ...
}
Related