nickylin / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

Add support for doing verifyNew without having explicitly expected any #248

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This should work:

public class Test1 {
    public void doit() {
        new Test2();
    }
}

public class Test2 {

}

and a test class :

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Test1.class)
public class Test1Test {

    private Test1 test1;

    @Test
    public void testDoit() throws Exception {
        test1 = new Test1();

        test1.doit();

        PowerMockito.verifyNew(Test2.class).withNoArguments();
    }
}

Original issue reported on code.google.com by johan.ha...@gmail.com on 25 Mar 2010 at 12:45

GoogleCodeExporter commented 9 years ago
Issue 244 has been merged into this issue.

Original comment by johan.ha...@gmail.com on 25 Mar 2010 at 5:50

GoogleCodeExporter commented 9 years ago
I've investigated this and it'll require big internal changes in PowerMock. 
MockGateway or the MockRepository may need to be able to create mocks 
dynamically 
when no NewInvocationSubsitute was found OR each extension API need to be able 
to 
implement there own MockRepository. I.e. refactor mock repo to an interface and 
let 
the extension API provide it to PowerMock on bootstrap. The bootstrapper must 
then 
inject the impl to the mock gateway and other places where's used for each 
test. I'm 
changing this to future. It's probably a nice feature workarounds exist. 

1) Use an ArgumentCaptor in Mockito or a Capture in EasyMock if the new 
instance is 
passed as a parameter to a method.
2) Setup the expectation and return a mock using PowerMock/PowerMockito.

Original comment by johan.ha...@gmail.com on 25 Mar 2010 at 7:48

GoogleCodeExporter commented 9 years ago
Another use case should be consided:
In the testDoIt() method, I would like to verify that constructor was never 
called...
    PowerMockito.verifyNew(Test2.class, never()).withNoArguments();

Original comment by yannick....@telecomnancy.net on 29 Jul 2011 at 12:19