0

We are creating a RestTemplate object inside the method, I want to mock that object.

Tried using powermockito but getting an exception, tried different way to mock the object but still helpless.

org.mockito.exceptions.base.MockitoException: Field 'eAPIRestService' annotated with @InjectMocks is null.
Please make sure the instance is created *before* MockitoAnnotations.initMocks();
Example of correct usage:
   class SomeTest {
      @InjectMocks private Foo foo = new Foo();

      @Before public void setUp() {
         MockitoAnnotations.initMock(this);

    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:276)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

and here is my test case where I am mocking resttemplate

@RunWith(MockitoJUnitRunner.class)
public class Test{

    @InjectMocks
    private MyService myService

    RestTemplate restTemplate;

    public Test() throws Exception {
        restTemplate = new RestTemplate();
        PowerMockito.whenNew(RestTemplate.class).withNoArguments().thenReturn(restTemplate);
    }
  ..........
}
Ravat Tailor
  • 1,193
  • 3
  • 20
  • 44
  • Not an answer, but some advise: do yourself a favor and walk away from PowerMockito. For the most part, it undoes the promise that unit tests encourage better production code. There's probably one or two useful things PowerMockito can do (`whenNew` is not one of them) but once you have it on the classpath, people will start doing 99 other, bad things with it. You could maybe make `RestTemplate` a Spring dependency, and use `@Spy` in the test instead, or just `@Mock` it altogether. Your question seems a duplicate of this, though: https://stackoverflow.com/questions/25317804 – Sander Verhagen Jan 13 '19 at 09:47

1 Answers1

0

The error message has the answer for you actually.

@InjectMocks
private MyService myService = new MyService();

should fix your problem.

Slav Pilus
  • 197
  • 1
  • 10