@RunWith(PowerMockRunner.class)
@PrepareForTest(TestNewConstructor.class)
public class TestNewConstructor {
public String methodCallNew() {
ConstructorDemo test = new ConstructorDemo("abc");
return test.getParam(); // should be always abc
}
@Test
public void testMockNewOperator() throws Exception {
String expectedValue = "mocked value";
// we use mockito to setup behavior of returned object
ConstructorDemo mock = Mockito.mock(ConstructorDemo.class);
Mockito.when(mock.getParam()).thenReturn(expectedValue);
// we use expectNew to return the mockito-created object
PowerMock.expectNew(ConstructorDemo.class, "abc").andReturn(mock);
PowerMock.replayAll();
TestNewConstructor test = new TestNewConstructor();
Assert.assertEquals(expectedValue, test.methodCallNew());
PowerMock.verifyAll();
}
}
I hope that helps.