英文:
Is there anything like Lombok @AllArgConstructor for @MockBeans?
问题
我在我的Java项目中使用Project Lombok。
我的Controller看起来像这样:
@RestController
@AllArgsConstructor
public class UserController {
private RegisterService registerService;
private UserService userService;
private UserValidator userValidator;
private LoginService loginService;
/*
* 控制器的其余部分
*/
}
因此,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;
/*
* 控制器测试的其余部分
*/
}
只是为了使编程更少地编写大量代码,是否有类似于@AllMockArgConstructor
的东西?
或者有没有办法,我不必总是添加所有的服务?
如果这是一个愚蠢的问题,请解释一下为什么。
提前感谢!
英文:
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
得分: 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 {
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论