Java Powermock + Mockito 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20754432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:05:57  来源:igfitidea点击:

Powermock + Mockito not working

javamockitopowermock

提问by Jazzepi

Trying to use Powermock to mock out a static method on SystemTray. Not sure why this isn't working. I've checked the match of Powermock -> Mockito versions, and I think I've followed all the steps for adding the right annotations, and using the correct PowerMock methods to setup the static one.

尝试使用 Powermock 模拟 SystemTray 上的静态方法。不知道为什么这不起作用。我检查了 Powermock -> Mockito 版本的匹配,我想我已经按照所有步骤添加了正确的注释,并使用正确的 PowerMock 方法来设置静态注释。

The static method on SystemTray seems to be called without the stubbed functionality set by the when().

SystemTray 上的静态方法似乎在没有 when() 设置的存根功能的情况下被调用。

I am mixing Powermock and Mockito calls here, but according to the docs that is correct.

我在这里混合了 Powermock 和 Mockito 调用,但根据正确的文档。

package CommissionChecker;

import org.apache.commons.logging.Log;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.util.ReflectionTestUtils;

import java.awt.*;
import java.io.IOException;
import java.util.List;

import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SystemTray.class)
public class DisplayManagerTest {

    @Mock
    Log logMock;
    @Mock
    Runner runnerMock;

    @Test
    public void display_manager_does_nothing_if_system_tray_is_not_supported() throws IOException, AWTException {
        mockStatic(SystemTray.class);
        when(SystemTray.isSupported()).thenReturn(false);

        new DisplayManager(runnerMock);

        verifyZeroInteractions(runnerMock);
    }
}

These are my maven dependencies

这些是我的 Maven 依赖项

    <powermock.version>1.5.2</powermock.version>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>

采纳答案by Jazzepi

Just needed to change this line

只需要改变这一行

@RunWith(PowerMockRunner.class)

to

@RunWith(DisplayManager.class)

According to this https://code.google.com/p/powermock/wiki/MockSystem

根据这个https://code.google.com/p/powermock/wiki/MockSystem

回答by user4308864

Here is a simple example using PowerMock:

这是一个使用 PowerMock 的简单示例:

package test;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.testng.Assert.*;

import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.testng.IObjectFactory;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;

import demo.powermock.IdGenerator;
import demo.powermock.ServiceRegistartor;
//import org.easymock.classextension
@RunWith(PowerMockRunner.class)
@PrepareForTest(IdGenerator.class)
public class Test111 {
    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new org.powermock.modules.testng.PowerMockObjectFactory();
    }

    @Test
    //@org.testng.annotations.Test
    public void testRegisterService() throws Exception {
        long expectedId = 42;

        // We create a new instance of test class under test as usually.
        ServiceRegistartor tested = new ServiceRegistartor();

        // This is the way to tell PowerMock to mock all static methods of a
        // given class
        PowerMock.mockStatic(IdGenerator.class);

        /*
         * The static method call to IdGenerator.generateNewId() expectation.
         * This is why we need PowerMock.
         */
        expect(IdGenerator.generateNewId()).andReturn(expectedId);

        // Note how we replay the class, not the instance!
        PowerMock.replay(IdGenerator.class);

        long actualId = new ServiceRegistartor().registerService();

        // Note how we verify the class, not the instance!
        PowerMock.verify(IdGenerator.class);

        // Assert that the ID is correct
        assertEquals(expectedId, actualId);
    }
}

回答by Hunsu

I had the same problem but I added the import manually the problem disappeared.

我遇到了同样的问题,但我手动添加了导入,问题消失了。

import org.powermock.modules.junit4.PowerMockRunner;