wood burning stoves 2.0*
The moose likes Testing and the fly likes Spring Injection and Powermockito Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Spring Injection and Powermockito" Watch "Spring Injection and Powermockito" New topic
Author

Spring Injection and Powermockito

Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
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

Joined: Sep 24, 2008
Posts: 30
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.
 
wood burning stoves
 
subject: Spring Injection and Powermockito
 
Similar Threads
JUnit 4 and @RunAs annotation
integrating SpringContextTests with PowerMock
How to write JUnit test
Spring 3 in Action
Automatic Mocking with Spring framework