简体   繁体   English

Powermock + Mockito无法正常工作

[英]Powermock + Mockito not working

Trying to use Powermock to mock out a static method on SystemTray. 尝试使用Powermock在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-> 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>

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

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);
    }
}

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

import org.powermock.modules.junit4.PowerMockRunner;

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

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