您好,我正在尝试将 PowerMockito 和 Mockito 与 @SpringJUnit4ClassRunner.class 一起使用。我不确定哪里有冲突。我设法只使用了 mockito ok 但需要使用 PowerMockito 进行静态分析。
获得以下跟踪:
org.mockito.exceptions.misusing.UnfinishedStubbingException:在此处检测到未完成的存根:-> 在 org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:31)
...
测试代码的基本部分如下:
失败出现在第 2 行。如果删除第 1 行和第 3 行,则测试正常。如果将 SpringJUnit4ClassRunner.class 替换为 PowerMockRunner.class,则该测试也有效。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@PrepareForTest({StatFactory.class})
public class TestMain {
@Test
public void testGetStatic() throws Exception {
PowerMockito.mockStatic(StatFactory.class); // 1
StatsFactory sf = mock(StatsFactory.class);
Main main = new Main();
PowerMockito.doReturn(false).when(StatFactory.class, "testFunction"); // 3
Mockito.when(sf.ok()).thenReturn(5); // 2
boolean ret = main.getData();
}
pom.xml
...
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.1.0.RELEASE</org.springframework-version>
<powermock.version>1.4.9</powermock.version>
</properties>
...
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<type>jar</type>
<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>
...