• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring Injection and Powermockito

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic