3

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!

Joergi
  • 1,527
  • 3
  • 39
  • 82

1 Answers1

1

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 {
   ...
}
Daniel Taub
  • 5,133
  • 7
  • 42
  • 72
  • Spring `@MockBean` is more or less only a wrapper over Mocktio `@Mock`... can it be done with `@Mock`? – Joergi Sep 07 '20 at 09:15
  • Thanks a lot, I accepted your answer, even it was not the one I wanted to read :-( I will read the information you gave me. – Joergi Sep 07 '20 at 09:42