Java 对于@MockBeans是否有类似Lombok@AllArgConstructor的东西?
我在Java项目中使用 我的控制器如下所示:Java 对于@MockBeans是否有类似Lombok@AllArgConstructor的东西?,java,mocking,mockito,lombok,Java,Mocking,Mockito,Lombok,我在Java项目中使用 我的控制器如下所示: @RestController @AllArgsConstructor public class UserController { private RegisterService registerService; private UserService userService; private UserValidator userValidator; private LoginService loginService
@RestController
@AllArgsConstructor
public class UserController {
private RegisterService registerService;
private UserService userService;
private UserValidator userValidator;
private LoginService loginService;
/*
* rest of the controller
*/
}
@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
*/
}
因此控制器必须如下所示:
@RestController
@AllArgsConstructor
public class UserController {
private RegisterService registerService;
private UserService userService;
private UserValidator userValidator;
private LoginService loginService;
/*
* rest of the controller
*/
}
@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
*/
}
为了减少编程中的代码猴子,有没有类似于@AllMockArgConstructor的东西?或者,我不必总是添加所有服务 如果这是一个愚蠢的问题,请解释为什么
提前谢谢 不幸的是,这无法做到 注释目标仅应用于字段和类型,而不应用于方法,您可以阅读有关它的更多信息
如果
@MockBeans
能够支持其他方法,那么可以通过以下方式完成:
@Getter(onMethod_={@MockBean} )
@WebMvcTest(UserController.class)
public class UserControllerTest {
...
}
Spring
@MockBean
或多或少只是Mocktio@Mock
上的一个包装器。。。可以用@Mock
完成吗?非常感谢,我接受了你的答案,即使它不是我想读的答案:-(我会读你给我的信息。