运行 Junit & PowerMock 与 Mockito 通过 PowerMockRunner 从 maven

发布于 2024-08-12 08:24:22 字数 3839 浏览 16 评论 0原文

我无法通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

<逆流佳人身旁 2024-08-19 08:24:22

我刚刚遇到了这个错误并解决了这个问题。我的 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 来理解关联的依赖关系。

I just had this error and worked through the solution. My pom.xml file had the following dependency:

<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!!!

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.

别念他 2024-08-19 08:24:22

我无法重现你的问题。在我的 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>

以及以下测试类(跳过导入):

@RunWith(PowerMockRunner.class)
@PrepareForTest( { App.class })
public class AppTest {
    @Test
    public void testApp() {
        assertTrue(true);
    }
}

运行 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] ------------------------------------------------------------------------

所以问题是: AlertNotificationsTest 中是否有用 @Test 注释的方法?

I can't reproduce your problem. With the following content in my 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:

...
-------------------------------------------------------
 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?

尽揽少女心 2024-08-19 08:24:22

我也遇到过这个问题,但这不是 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.

も星光 2024-08-19 08:24:22

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 ?

剑心龙吟 2024-08-19 08:24:22

如果你查看 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.

鸢与 2024-08-19 08:24:22

我也遇到了同样的问题,我花了一段时间才弄清楚。我的设置引入了旧版本的 jboss.javassist,这奇怪地阻止了 PowerMockRunner 的工作。

值得注意的是,我还有一个混合的 JUnit / TestNG 环境。我之前尝试过添加多个 Surefire 提供程序的解决方案,但这也不起作用(使用 Surefire 2.14.1)。升级到 Surefire 2.17 后,我的 JUnit 和 TestNG 测试都开始运行,无需声明任何 Surefire 提供程序。

这是我的插件部分......

        <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>

以及相关的测试部门......

    <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>

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...

        <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>
笑咖 2024-08-19 08:24:22

混合 TestNG 和 TestNG 时出现问题。 JUnit 测试。将所有测试迁移到 Junit 解决了我的问题。
谢谢你们。

There was a problem when mixing both TestNG & JUnit tests. Migrating all tests to Junit solved my problem.
Thanks guys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文