How should I use EasyMock's @Mock annotation (new in version 3.2)?

独自空忆成欢 提交于 2019-12-08 17:49:24

Examining their source code, they are using reflection to inject anything with an @Mock into a field of the @TestSubject. Their javadoc for the method

public static void injectMocks(final Object obj)

in EasyMockSupport.java says:

Inject a mock to every fields annotated with {@link Mock} on the class passed in parameter. Then, inject these mocks to the fields of every class annotated with TestSubject.

The rules are

  • Static and final fields are ignored
  • If a mock can be assigned to a field, do it. The same mock an be assigned more than once
  • If no mock can be assigned to a field, skip it silently
  • If two mocks can be assigned to the same field, return an error
  • Fields are searched recursively on the superclasses

Note: If the parameter extends EasyMockSupport, the mocks will be created using it to allow replayAll/verifyAll to work afterwards

@param obj the object on which to inject mocks

@since 3.2

public static void injectMocks(final Object obj) { ... }

For you to use the @Mock annotation, you would need a @TestSubject that has an HttpServletRequest field for EasyMock to set the @Mock on (via reflection). The annotations are provided to make it a little easier to wire up a test, it let's you skip the createMock, and then calling the settter yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!