3

I have SomeService that uses a @Resource annotation for EJB TimerService.

@Resource private TimerService timerService;

How can I inject a mock TimerService in place of this TimerService in SomeService?

user840930
  • 5,214
  • 21
  • 65
  • 94

1 Answers1

1

If you're using mockito: Mockito can inject annotated fields for you see: @RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this)

I would suggest instead of using annotations on fields to use a annotated constructor. That way you can make all injected fields final and you will get build failures when dependencies change instead of runtime/test failures/exceptions. And the added bonus is that you can just call the constructor in your tests and inject the mocks that way.

Community
  • 1
  • 1
Bob Brinks
  • 1,372
  • 1
  • 10
  • 19