在我的单元测试中,powermockrunner尚未确认,尽管我在pom中需要所有依赖项,并且都下载了所有必需的罐子。我正在尝试测试一种静态方法。我不断获得错误"Type mismatch: cannot convert from Class<PowerMockRunner> to Class<? extends Runner>"
@RunWith(PowerMockRunner.class)
。为什么现在已经认可PowerMockrunner?我很确定我所有的依赖项都已添加。我正在使用junit4。这是我的pom文件:
注意:这不是重复的帖子。我的问题承认,为了运行PowerMock,需要某些依赖性,但是这个特定的问题问,为什么在添加了所有这些依赖项后,PowerMockrunner.Class仍然无法识别。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vismark</groupId>
<artifactId>TestingTraining</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestingTraining</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这是我的简单测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest(UserDAO.class)
public class UserDAOTest {
@Test
public void createShouldReturnUserId() {
UserDAO dao = new UserDAO();
PowerMockito.mockStatic(UserDAO.class);
when(IDGenerator.generateId()).thenReturn(1);
int result = dao.create(new User());
assertEquals(1, result);
}
}
尝试添加powermock-module-junit4-common
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-common</artifactId>
<version>1.6.4</version>
</dependency>