I'm try to mock a class that has some public methods with annotations (it's actually a SpringBoot @RestController type of class).
When I mock it and use expect
for few of the methods that have annotations I see that Easymock loses the annotation and my code breaks.
Is that EasyMock limitation or I need to do something to make it work?
EDIT: Here is a snippet of the class I'm trying to mock:
@CrossOrigin
@RestController
@RequestMapping(path = "/users")
public class UsersController {
@RequestMapping(
path = "/getUsers",
method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public List<User> getUsers(@RequestBody UserRequest userRequest, HttpServletRequest request) {
...
}
...
}
Mocking Test Code:
@Test
public void testGetUsers() throws Exception {
UserController controller = createMock(UserController.class);
expect(controller.getUsers(userRequest, httpRequest)).andReturn(Collections.emptyList());
replay(controller);
...
verify(controller);
}
I did a simple check and instead of passing the mocked instance I passed an instantiated instance and it worked well. so my conclusion is that somehow when mocking a class that has annotations on methods it loses the annotations.
btw, I'm using easymock 3.4
EDIT 2:
I've upgrade to v4.2 but it didn't help.
The way to reproduce it is simply by running this code
public void testGetUsers() throws Exception {
UserController controller = createMock(UserController.class);
expect(controller.getUsers(userRequest,httpRequest)).andReturn(Collections.emptyList());
replay(controller);
Method[] methods = controller.getClass().getMethods();
if(methods[0].isAnnotationPresent(RequestMapping.class) == false) {
System.out.println("FOUND THE PROBLEM!!");
}
Thank you!
A mock is a simple thing. It extends a class and overrides all the methods. The annotations are not added to the generated class. Because you are mocking a behavior. The mock is not meant to be passed to an annotation processor.
Nothing is preventing from doing it, it's just that nobody has needed that feature for 20 years. I am curious to see a full example of why you need it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.