有没有类似于 Lombok 的 @AllArgsConstructor,用于 @MockBeans?

huangapple go评论77阅读模式
英文:

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 {
   ...
}

huangapple
  • 本文由 发表于 2020年9月7日 07:29:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63769756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定