1

MyClass.java:

package test;
public final class MyClass {
    public MyClass() { }
    public Package returnPackage() {
        return MyClass.class.getPackage();
    }
}

TestClass.java:

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

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class TestClass {
    @Test
    public void test() throws Exception {
        System.out.println(new MyClass().returnPackage());
        mockStatic(MyClass.class);
        System.out.println(new MyClass().returnPackage());
    }
}

However, this results in:

package test
null

Any MyClass.class method call returns null after PowerMockito.mockStatic(Class). Is there any way to prevent this?

4

1 に答える 1

0

回避策: EasyMock-API を使用して、.xml なしで静的モックをセットアップできますPowerMockito.mockStatic()

于 2012-08-13T17:22:56.967 に答える