运行 Junit & PowerMock 与 Mockito 通过 PowerMockRunner 从 maven
我无法通过 Maven 运行 Powermock。我是用于驱动 jUnit 测试的 PowerMock Mockito 和 PowerMockRunner。
这是测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest( { UserLocalServiceUtil.class, ExpandoBridge.class })
public class AlertNotificationsTest {
//...
我没有配置任何特殊的东西来运行测试。我的 pom 引用了以下 deps:
- org.mockito |全部模拟 | 1.8.0
- 君单元 |朱尼特 | 4.6.0
- org.powermock.modules | powermock-module-junit4 | powermock-module-junit4 | 1.3.1
- org.powermock.api | powermock-api-mockito | 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组合有问题。
我希望能够同时运行两者,但如果我找不到办法,我会去回答我自己的问题。 谢谢各位
I am not being able to run Powermock through maven. I'm the PowerMock Mockito and PowerMockRunner for driving a jUnit test.
Here's the test:
@RunWith(PowerMockRunner.class)
@PrepareForTest( { UserLocalServiceUtil.class, ExpandoBridge.class })
public class AlertNotificationsTest {
//...
I haven't configured anyting special for running the test. My pom references the following deps:
- org.mockito | mockito-all | 1.8.0
- junit | junit | 4.6.0
- org.powermock.modules | powermock-module-junit4 | 1.3.1
- org.powermock.api | powermock-api-mockito | 1.3.1
when I run mvn -Dtest=AlertNotificationsTest test
mvn says there's no test to run.
But if I run the same test class from eclipse, everything runs ok.
Am I doing something wrong?
Here's my pom.xml below (the relevant parts)
<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>
Here's the output from 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] ------------------------------------------------------------------------
Note: I can run other tests, I just can't run this test.
If I make the AlertNotificationsTest
class extend junit.framework.TestCase
the class gets picked up by maven, but it seems that it does not get driven by PowerMockRunner
.
Here's the output of that:
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
Again, these tests run just fine with Eclipse.
Update I found a possible problem & workaround. I have tests with TestNG and JUnit. If I remove TestNG from my pom and migrate all my tests to JUnit, I am able to run my PowerMock test with mvn test
. So it seems that there's a problem with maven and the junit/testng combo.
I'd like to be able to run both, but If I don't find a way I'll go and answer my own question.
Thanks guys&gals
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我刚刚遇到了这个错误并解决了这个问题。我的 pom.xml 文件具有以下依赖项:
问题来自于我的代码使用 JUnit 并且上述依赖项具有对 TestNG 的外部依赖项。这阻止了我的测试运行。为什么我不知道 - 你可能会认为测试框架会被测试得更好一点!
无论如何,解决方案是将“完整”依赖项分解为所需的依赖项:
这解决了它。顺便说一句,我使用
mvn dependency:tree
来理解关联的依赖关系。I just had this error and worked through the solution. My pom.xml file had the following dependency:
The problem comes from the fact my code uses JUnit and the above dependency has an external dependency on TestNG. This was stopping my test from running. Why I don't know - you would have though a test framework would have been tested a little bit better!!!
Anyway the solution was to break down the 'full' dependencies to just those required:
That solved it. BTW I used
mvn dependency:tree
to understand the associated dependencies.我无法重现你的问题。在我的 pom.xml 中包含以下内容:
以及以下测试类(跳过导入):
运行 mvn test -Dtest=AppTest 可以正常工作并给出以下输出:
所以问题是:
AlertNotificationsTest
中是否有用@Test
注释的方法?I can't reproduce your problem. With the following content in my pom.xml:
And the following test class (skipping the imports):
Running
mvn test -Dtest=AppTest
just works fine and give me the following output:So the question is: do you have a method annotated with
@Test
inAlertNotificationsTest
?我也遇到过这个问题,但这不是 PowerMock 问题。我的测试类名为 XStaticTests.java。
当我运行“mvn clean test”时,此测试不会运行,它仅在我使用“-Dtest=...”指定测试时运行
Surefire 文档提到默认情况下仅搜索这些模式:
“/Test*.java” - 包括其所有子目录以及以“Test”开头的所有 java 文件名。
“/Test.java” - 包括其所有子目录以及所有以“Test”结尾的 java 文件名。
“*/*TestCase.java” - 包括其所有子目录以及以“TestCase”结尾的所有 java 文件名。
因此,将类名更改为以其中之一结尾的类名,将在调用“mvn test”时运行,否则需要使用类名专门配置 Surefire 插件。
I came across this issue as well, but its not a PowerMock issue. My Test class was named XStaticTests.java.
When I run "mvn clean test", this test would not run, it only ran when I specified the test using "-Dtest=..."
The surefire documentation mentions that by default only these patterns are searched for :
"/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test".
"/Test.java" - includes all of its subdirectories and all java filenames that end with "Test".
"*/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".
Therefore changing the classname to one that ends with one of these, will run when "mvn test" is called, else the surefire plugin needs to be configured with the class name specifically.
Powermock 设置对我来说看起来不错,并且 jar 看起来不错(假设 Maven 传递依赖项获取其他 powermock jar - 在我们的 ivy 解析获取它们后,我们大约有 6-7 个)
Eclipse 可能正在使用它自己的“内部”JUnit 库,因此不同的行为?
测试是否用 org.junit.@Test 注释?
Powermock setup looks Ok to me, and the jars seem fine (assuming maven transitive dependencies get the other powermock jars - we have about 6-7 after our ivy resolve gets them)
Eclipse might be using it's own "internal" JUnit library, thus the different behaviours ?
Are the test annotated with org.junit.@Test ?
如果你查看 Surefire 插件的源代码,你会发现它做了一些偷偷摸摸的事情。如果它在类加载器中找到任何 TestNG 包,它将选择运行 TestNG TestRunner。我还没有看到任何 JUNit 和 TestNG 测试并行运行良好的示例。
If you look into the source of the Surefire plugin, it does some sneaky stuff. If it finds any TestNG packages in the Classloader, it will opt to run a TestNG TestRunner. I haven't yet seen any examples of both JUNit and TestNG tests running well side-by-side.
我也遇到了同样的问题,我花了一段时间才弄清楚。我的设置引入了旧版本的 jboss.javassist,这奇怪地阻止了 PowerMockRunner 的工作。
值得注意的是,我还有一个混合的 JUnit / TestNG 环境。我之前尝试过添加多个 Surefire 提供程序的解决方案,但这也不起作用(使用 Surefire 2.14.1)。升级到 Surefire 2.17 后,我的 JUnit 和 TestNG 测试都开始运行,无需声明任何 Surefire 提供程序。
这是我的插件部分......
以及相关的测试部门......
I had the same problem, and it took me a while to figure out. My setup was pulling in an older version of jboss.javassist, which oddly was preventing the PowerMockRunner from working at all.
It's worth noting that I also have a mixed JUnit / TestNG environment. I previously tried the solution of adding multiple surefire providers, and that didn't work either (using surefire 2.14.1). After upgrading to surefire 2.17, both my JUnit and TestNG tests started running without needing to declare any surefire providers.
Here's my plugin section...
... and the relevant testing deps ...
混合 TestNG 和 TestNG 时出现问题。 JUnit 测试。将所有测试迁移到 Junit 解决了我的问题。
谢谢你们。
There was a problem when mixing both TestNG & JUnit tests. Migrating all tests to Junit solved my problem.
Thanks guys.