Java 从 maven 通过 PowerMockRunner 使用 Mockito 运行 Junit 和 PowerMock
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1796833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running Junit & PowerMock with Mockito through PowerMockRunner from maven
提问by Miguel Ping
I am not being able to run Powermock through maven. I'm the PowerMock Mockito and PowerMockRunner for driving a jUnit test.
我无法通过 maven 运行 Powermock。我是用于驱动 jUnit 测试的 PowerMock Mockito 和 PowerMockRunner。
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:
我没有为运行测试配置任何特殊的东西。我的 pom 引用了以下 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
- 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.
当我运行mvn -Dtest=AlertNotificationsTest test
mvn 时说没有要运行的测试。但是如果我从 eclipse 运行相同的测试类,一切运行正常。
Am I doing something wrong?
难道我做错了什么?
Here's my pom.xml below (the relevant parts)
下面是我的 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>
Here's the output from maven
这是 maven 的输出
mvn -Dtest=AlertNotificationsTest test
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] ------------------------------------------------------------------------
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
.
注意:我可以运行其他测试,但我无法运行此测试。如果我让AlertNotificationsTest
该类扩展junit.framework.TestCase
该类会被 maven 接收,但它似乎不是由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.
同样,这些测试在 Eclipse 上运行得很好。
UpdateI 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.
更新我发现了一个可能的问题和解决方法。我用 TestNG 和 JUnit 进行了测试。如果我从 pom 中删除 TestNG 并将所有测试迁移到 JUnit,我就可以使用mvn test
. 所以看起来maven和junit/testng组合有问题。
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
我希望能够同时运行两者,但如果我找不到方法,我会回答我自己的问题。谢谢你们
采纳答案by Miguel Ping
There was a problem when mixing both TestNG & JUnit tests. Migrating all tests to Junit solved my problem. Thanks guys.
混合 TestNG 和 JUnit 测试时出现问题。将所有测试迁移到 Junit 解决了我的问题。谢谢你们。
回答by Phantomwhale
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)
Powermock 设置对我来说看起来不错,罐子看起来很好(假设 maven 传递依赖获得其他 powermock 罐子 - 在我们的常春藤解决方案获得它们之后,我们大约有 6-7 个)
Eclipse might be using it's own "internal" JUnit library, thus the different behaviours ?
Eclipse 可能正在使用它自己的“内部”JUnit 库,因此不同的行为?
Are the test annotated with org.junit.@Test ?
测试是否用 org.junit.@Test 注释?
回答by Pascal Thivent
I can't reproduce your problem. With the following content in my pom.xml:
我无法重现你的问题。在我的 pom.xml 中有以下内容:
<repositories>
<repository>
<id>powermock-repo</id>
<url>http://powermock.googlecode.com/svn/repo/</url>
</repository>
</repositories>
<properties>
<powermock.version>1.3.1</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock.modules</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock.api</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<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>
</dependency>
</dependencies>
And the following test class (skipping the imports):
以及以下测试类(跳过导入):
@RunWith(PowerMockRunner.class)
@PrepareForTest( { App.class })
public class AppTest {
@Test
public void testApp() {
assertTrue(true);
}
}
Running mvn test -Dtest=AppTest
just works fine and give me the following output:
运行运行mvn test -Dtest=AppTest
正常,并给我以下输出:
... ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.mycompany.app.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.135 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3 seconds [INFO] Finished at: Wed Nov 25 17:34:32 CET 2009 [INFO] Final Memory: 9M/79M [INFO] ------------------------------------------------------------------------
So the question is: do you have a method annotated with @Test
in AlertNotificationsTest
?
所以问题是:你有一个用@Test
in注释的方法AlertNotificationsTest
吗?
回答by Nelz
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.
如果你查看 Surefire 插件的来源,它会做一些鬼鬼祟祟的事情。如果它在类加载器中找到任何 TestNG 包,它将选择运行 TestNG TestRunner。我还没有看到 JUNit 和 TestNG 测试并排运行良好的任何示例。
回答by shark8me
I came across this issue as well, but its not a PowerMock issue. My Test class was named XStaticTests.java.
我也遇到过这个问题,但它不是 PowerMock 问题。我的测试类被命名为 XStaticTests.java。
When I run "mvn clean test", this test would not run, it only ran when I specified the test using "-Dtest=..."
当我运行“mvn clean test”时,这个测试不会运行,它只在我使用“-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".
surefire 文档提到默认情况下只搜索这些模式:“/ Test*.java” - 包括其所有子目录和所有以“Test”开头的 java 文件名。“/ Test.java” -包括所有的子目录和所有的Java文件名以“测试”结束。“*/*TestCase.java” - 包括其所有子目录和所有以“TestCase”结尾的 java 文件名。
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.
因此,将类名更改为以其中之一结尾的类名,将在调用“mvn test”时运行,否则需要使用类名专门配置surefire插件。
回答by Adam Davies
I just had this error and worked through the solution. My pom.xml file had the following dependency:
我刚刚遇到了这个错误并解决了这个问题。我的 pom.xml 文件具有以下依赖项:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.5</version>
<classifier>full</classifier>
<scope>test</scope>
</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!!!
问题来自我的代码使用 JUnit 并且上述依赖项对 TestNG 有外部依赖项。这阻止了我的测试运行。为什么我不知道 - 你会认为测试框架会更好地测试!!!
Anyway the solution was to break down the 'full' dependencies to just those required:
无论如何,解决方案是将“完整”依赖项分解为所需的那些:
<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>
That solved it. BTW I used mvn dependency:tree
to understand the associated dependencies.
那解决了它。顺便说一句,我曾经mvn dependency:tree
了解相关的依赖关系。
回答by Jeff Fairley
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.
我遇到了同样的问题,我花了一段时间才弄清楚。我的设置引入了旧版本的 jboss.javassist,奇怪的是它完全阻止了 PowerMockRunner 工作。
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.
值得注意的是,我还有一个混合的 JUnit/TestNG 环境。我以前尝试过添加多个surefire提供程序的解决方案,但也不起作用(使用surefire 2.14.1)。升级到surefire 2.17 后,我的JUnit 和TestNG 测试都开始运行,无需声明任何surefire 提供程序。
Here's my plugin section...
这是我的插件部分...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<groups>spring, unit, integration</groups>
<systemPropertyVariables>
<java.awt.headless>true</java.awt.headless>
<org.apache.activemq.default.directory.prefix>target/test/</org.apache.activemq.default.directory.prefix>
<log4j.configuration>file:${project.basedir}/src/test/resources/log4j.properties</log4j.configuration>
</systemPropertyVariables>
<argLine>${surefire.args}</argLine>
</configuration>
</plugin>
... and the relevant testing deps ...
... 以及相关的测试部门 ...
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<!--
PowerMock versions are compatible with specific Mockito versions.
https://code.google.com/p/powermock/wiki/MockitoUsage13
-->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
<!-- without this PowerMock tests don't run in maven -->
<dependency>
<groupId>jboss</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
<scope>test</scope>
</dependency>