简体   繁体   English

模拟静态Liferay方法

[英]Mocking static Liferay method

I'm trying to mock the PortalUtil.getPortal() method like so 我正在尝试像这样模拟PortalUtil.getPortal()方法

PowerMock.mockStatic(PortalUtil.class);
Portal mockPortal = Mockito.mock(Portal.class);
Mockito.when(PortalUtil.getPortal()).thenReturn(mockPortal);

I'm getting the below error 我收到以下错误

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
It is a limitation of the mock engine.

I know Mockito cannot mock static methods but I'm also using PowerMock which is supposed to make this possible. 我知道Mockito无法模拟静态方法,但我也在使用PowerMock,它应该可以实现这一点。 I also tried using PowerMockito.mockStatic() instead of PowerMock.mockStatic() 我也尝试使用PowerMockito.mockStatic()代替PowerMock.mockStatic()

I have the below annotations at class level 我在课堂上有以下注释

@RunWith(PowerMockRunner.class)
@PrepareForTest(PortalUtil.class)

What am I missing? 我想念什么?

After using this bit of code I stopped getting NPE 使用这段代码后,我停止获取NPE

Portal mockPortal = Mockito.mock(Portal.class);
new PortalUtil().setPortal(mockPortal);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM