我试图使用PowerMockito为java.time.ZonedDateTime创建模拟,并期待ZonedDateTime的模拟对象。相反,实际的对象正在创建,因此我不能嘲笑ZonedDateTime类的方法。Powermock不为java.time.ZonedDateTime创建模拟
以下是我的代码片断
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ZonedDateTime.class})
public class ZonedDateTimeTest {
@Test
public void test(){
ZonedDateTime attribute = mock(ZonedDateTime.class);
when(attribute.format(any(DateTimeFormatter.class))).thenReturn("dummy");
//test code here
}
}
除此之外,当我尝试打印对象使用创建以下行 System.out.println(attribute.toString());
我得到异常以下:
java.lang.NullPointerException at java.time.ZonedDateTime.toString(ZonedDateTime.java:2208) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:124) at org.powermock.core.MockGateway.methodCall(MockGateway.java:185)
有人可以帮我解决这个问题吗?我应该创建一个GitHub问题吗?