Re: [mockito] How do I use PowerMockito with @injectmocks?

2,847 views
Skip to first unread message

Eric Lefevre-Ardant

unread,
Jul 10, 2012, 3:56:42 AM7/10/12
to moc...@googlegroups.com
hm... PowerMockito is really something maintained by the PowerMock people. Not sure this is the best place to get an answer to this question.

I have never used PowerMock, but my understanding is that it wouldn't be compatible with @Mock and @InjectMocks which are specific to Mockito. (personally, I do not like them anyway; I much prefer explicitly passing dependencies to the constructor of my class under test. This allows me to ignore situations that are tricky to understand such as the one you describe).

If you really want to mock a static method, then I'd recommend wrapping it with a normal class that will then be easily mocked.

For example, in my code, I have a Time class that looks like this:

public class Time {
  public long currentTimeMillis() {
    return System.currentTimeMillis();
  }
}

My class under test would then look like that:

public class UsefulClass {
  private final Time time;

  public UsefulClass() {
    this(new Time());
  }

  @VisibleForTesting
  UsefulClass(Time time) {
    this.time = time;
  }

  public void doSomething() {
    System.out.println(time.currentTimeMillis());
  }
}

I hope this helps.

On 10 July 2012 09:39, Rahul Bhatnagar <rhls.m...@gmail.com> wrote:

Hey I'm using Mockito and TestNG to write a unit test for a class that's making a lot of external calls to a service, I'm actually quite new to this hence I seem to be stuck with little documentation on the net for my exact problem.

my test looks like this basically

@Test
public class ClassToTestTest{

@Mock
private Object1 object1;

@Mock
private Object2 object2;

@InjectMocks
private ClassToTest classToTest;

public void test1(){
    classToTest.methodToTest();

}
...
...
}
 

The actual class is as follows

import FinalClass;

public class ClassToTest{

private Object1 object1;
private Object2 object2;

public void methodToTest(){
    object2 = FinalClass.getObject2();

    ...
    ...
}

...
...
}

 

I just need FinalClass.getObject2() to return the mock of Object2 That I've created in my Test, I know I can mock FinalClass using PowerMock, but I'm not quite getting how to inject it in the classToTest that I've created, so that when I run the classToTest.methodToTest() from my test object2 is initialized with my mocked implementation.

Thanks in Advance!

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/bfvrDHeFkXgJ.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.

Thirukka Karnan S

unread,
Apr 25, 2016, 5:56:05 AM4/25/16
to mockito
@InjectMocks respects static and final fields. Hence it will not work along with PowerMock while trying to inject those fields. I couldn't find an alternative to InjectMocks in PowerMock's annotations and so there isn't a way to overcome it.
Also, @InjectMocks fails silently even if the mocking is not successful! So it is better if you skip them for other alternatives
Reply all
Reply to author
Forward
0 new messages