【发布时间】:2010-12-20 06:57:36
【问题描述】:
我无法通过 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-module-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 测试
...
[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组合有问题。
我希望能够同时运行两者,但如果我找不到方法,我会去回答我自己的问题。 谢谢大家
【问题讨论】:
-
刚刚运行
mvn test时是否触发了测试(不扩展junit.framework.TestCase)? -
不,没有触发。
-
那么在使用
-Dtest时甚至不要指望它。您还有另一个问题(看起来您找到了源)。
标签: java maven-2 junit mockito powermock