• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring Injection and Powermockito

 
Michael Sampson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I spent some time searching on this and have some ideas on how to resolve my problem but thought I'd post this and see where it goes. So my problem is that I have a class I did not write but was trying to write the unit test for. It has private members which are DAO's. In the class under test the fields are annotated @Inject. Mocking those DAO's isn't hard. I was going to use the @Mock annotation and then @InjectMocks for the instance of the class under tetst. Now to do that, I know one way to do that is use the @RunWith(MockitoJunitRunner.class) and then another might be use the setUp method and do like a Spring Reflection util and call inject mocks or something. I am going to look into that further. A problem comes up that the method I am trying to test also has calls to a static method. Mocking static calls I am well familiar with but it also requires the @RunWith annotation but with PowerMockRunner class and of course I am pretty sure @RunWith can only take a single class. So I am trying to avoid updating the class under test, being that I didn't write it, but am not real sure how to resolve doing the injection of the mock objects and being able to mock out static method calls. Sorry if this was already asked but I thought I did a decent search through the forum.
 
Michael Sampson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I solved my own problem on this. Something I think I failed to mention in the original post was that the class under test that had the DAO member had no setter for that member. Being that I did mentio it was annotated with @Inject, I think that might have been inferred but on with the solution. So the DAO mock in the test is annotated with @Mock and the class under test is a member of the test class and it is annotated @InjectMocks. Inside the setUP method, there is a call MockitoAnnotation.initMocks(this) and it initializes the DAO mock object and is in injected into the class under test. The @RunsWith is thus available to be set to PowerMockRunner.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic