嘗試使用Powermock模擬SystemTray上的靜態方法。不知道爲什麼這不起作用。我檢查過Powermock - > Mockito版本的匹配,我想我已經按照所有步驟添加了正確的註釋,並使用正確的PowerMock方法來設置靜態的。Powermock + Mockito不工作
SystemTray上的靜態方法似乎在沒有由when()設置的存根功能的情況下被調用。
我在這裏混合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);
}
}
這些都是我的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>
code.google ..鏈接移到https://github.com/powermock/powermock – JonyD