'Mocking a object using powerMockito.whenNew() and using doNoting(object) but locally created object is calling actual method

public class A {
    public String[] processMessage(Message msg) {
        public boolean A(msg) {
            Utils utils = new Utils();
            utils.method(String);
            return false;
        }
    }
}

public class Utils {
    public void method(String s) {
        send messsage to AWS sqs;
    }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class ClassToTest {

    @Test
    public void testProcessMessage() {
        A a = new A();
        Utils utils = PowerMockito.mock(Utils.class);
        PowerMockito.whenNew(Utils.class).withNoArguments().thenReturn(utils);
        Mockito.doNothing().when(utils).method(anyString());
        String[] response = a.processMessage("Message");

    }
}

when running this test it is calling method utils.method(String) and throwing null pointer exception because it is trying to send message to AWS SQS from local

Cannot make code change to accept instance of Utils from constructor of class A



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source