我无法通过 Maven 运行 Powermock。我是用于驱动 jUnit 测试的 PowerMock Mockito 和 PowerMockRunner。
这是测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest( { UserLocalServiceUtil.class, ExpandoBridge.class })
public class AlertNotificationsTest {
//...
我没有为运行测试配置任何特殊的东西。我的 pom 引用了以下部门:
- org.mockito |模拟所有 | 1.8.0
- 联合 |联机 | 4.6.0
- org.powermock.modules | powermock-模块-junit4 | 1.3.1
- org.powermock.api | powermock-api-mockito | 1.3.1
当我运行 mvn -Dtest=AlertNotificationsTest test
时mvn 说没有要运行的测试。
但是如果我从 eclipse 运行相同的测试类,一切运行正常。
我做错了什么吗?
下面是我的 pom.xml(相关部分)
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.9</version>
<classifier>jdk15</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock.modules</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock.api</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
这是maven的输出
mvn -Dtest=AlertNotificationsTest test
...
[INFO] Surefire report directory: C:\Devel\Java\EP_PORTAL\information-provider\target\surefi
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.313 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)
[INFO] ------------------------------------------------------------------------
注意:我可以运行其他测试,我就是不能运行这个测试。
如果我制作 AlertNotificationsTest
类扩展junit.framework.TestCase
该类被 Maven 拾取,但它似乎没有被 PowerMockRunner
驱动.
这是它的输出:
Running TestSuite
[ERROR]: No test suite found. Nothing to run
Tests run: 4, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 1.053 sec <<< FAILURE!
Results :
Failed tests:
testSingleEventNotification(pt.estradasportugal.traffic.services.events.AlertNotificationsTest)
testTwoEventNotification(pt.estradasportugal.traffic.services.events.AlertNotificationsTest)
Tests run: 4, Failures: 2, Errors: 0, Skipped: 0
同样,这些测试在 Eclipse 上运行得很好。
更新 我发现了一个可能的问题和解决方法。我对 TestNG 和 JUnit 进行了测试。如果我从我的 pom 中删除 TestNG 并将我的所有测试迁移到 JUnit,我就能够使用 mvn test
运行我的 PowerMock 测试.所以看来 maven 和 junit/testng 组合有问题。
我希望能够同时运行两者,但如果我找不到方法,我会去回答我自己的问题。 谢谢各位大侠
最佳答案
我刚遇到这个错误并解决了这个问题。我的 pom.xml 文件具有以下依赖项:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.5</version>
<classifier>full</classifier>
<scope>test</scope>
</dependency>
问题来自于我的代码使用 JUnit 并且上述依赖项对 TestNG 具有外部依赖项。这阻止了我的测试运行。为什么我不知道 - 你会认为测试框架会被测试得更好一点!!!
无论如何,解决方案是将“完整”依赖项分解为仅需要的依赖项:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
这就解决了。顺便说一句,我使用 mvn dependency:tree
来理解相关的依赖关系。
关于java - 通过 Maven 的 PowerMockRunner 使用 Mockito 运行 Junit & PowerMock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1796833/