PowerMockito mockStatic 给出 MissingMethodInvocationException

PowerMockito mockStatic gives MissingMethodInvocationException

我正在尝试使用 PowerMockito 来 mockStatic 方法,我在其中尝试了 mockStatic 的一些选项以获得 class,导致不同的异常。

@RunWith(PowerMockRunner.class)
@PrepareForTest({Base64.class})
public class BqClientFactoryTest {
    @Test
    public void testGetBigQueryClient() throws Exception {
        mockStatic(Base64.class);
        Base64.Decoder mockDecoder = mock(Base64.Decoder.class);
        when(Base64.getDecoder()).thenReturn(mockDecoder);

这导致 org.mockito.exceptions.misusing.MissingMethodInvocationException:

我用了另一个这样的例子

@RunWith(PowerMockRunner.class)
@PrepareForTest({Base64.class})
public class BqClientFactoryTest {
    @Test
    public void testGetBigQueryClient() throws Exception {
        mockStatic(Base64.class);
        Base64.Decoder mockDecoder = mock(Base64.Decoder.class);
        doReturn(mockDecoder).when(Base64.class, "getDecoder");

这给了我

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here: 

如果我用

BDDMockito.given(Base64.getDecoder()).willReturn(mockDecoder);

Mocking static methods with Mockito 开始,它仍然 returns org.mockito.exceptions.misusing.MissingMethodInvocationException

我试图在 SO 上查看类似的问题,但它们似乎没有帮助。 任何解决此问题的帮助都将不胜感激。

我按照这个解决了它,所有其他解决方案都不适合我。很难搜索到这个解决方案,因为相同的SO上太多了。

, PowerMock, mock a static method, THEN call real methods on all other statics

@RunWith(PowerMockRunner.class)
@PrepareForTest({Base64.class})
public class BqClientFactoryTest {
    @Test
    public void testGetBigQueryClient() throws Exception {
        Base64.Decoder mockDecoder = mock(Base64.Decoder.class);
        stub(method(Base64.class, "getDecoder")).toReturn(mockDecoder);