【问题标题】:Running Junit & PowerMock with Mockito through PowerMockRunner from maven通过来自 maven 的 PowerMockRunner 使用 Mockito 运行 Junit 和 PowerMock
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 这个解决方案对我有用。我正在使用 Powermock 1.6.4 版本和 Mockito 1.9.5。
【解决方案2】:

我无法重现您的问题。在我的 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 -------------------------------------------------- ----- 运行 com.mycompany.app.AppTest 测试运行:1,失败:0,错误:0,跳过:0,经过时间:0.135 秒 结果 : 测试运行:1,失败:0,错误:0,跳过:0 [信息] --------------------------------------------- ------------------------- [信息] 构建成功 [信息] --------------------------------------------- ------------------------- [INFO] 总时间:3 秒 [INFO] 完成时间:2009 年 11 月 25 日星期三 17:34:32 CET [INFO] 最终内存:9M/79M [信息] --------------------------------------------- -------------------------

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

【讨论】:

  • 那么请发布您的pom.xml。正如我所说,我无法重现您描述的行为。顺便说一句,当你运行mvn test 时,你观察到了什么?
  • 已发布您要求的内容
【解决方案3】:

我也遇到过这个问题,但它不是 PowerMock 问题。我的测试类被命名为 XStaticTests.java。

当我运行“mvn clean test”时,这个测试不会运行,它只在我使用“-Dtest=...”指定测试时运行

surefire 文档提到默认情况下只搜索这些模式: “/Test*.java” - 包括其所有子目录和所有以“Test”开头的 java 文件名。 “/Test.java” - 包括其所有子目录和所有以“Test”结尾的 java 文件名。 "*/*TestCase.java" - 包括它的所有子目录和所有以 "TestCase" 结尾的 java 文件名。

因此将类名更改为以其中之一结尾的类名,将在调用“mvn test”时运行,否则需要专门为surefire插件配置类名。

【讨论】:

    【解决方案4】:

    Powermock 设置在我看来还不错,而且 jar 看起来也不错(假设 maven 传递依赖项获得了其他 powermock jar - 在我们的 ivy 解析获得它们之后,我们大约有 6-7 个)

    Eclipse 可能正在使用它自己的“内部”JUnit 库,因此会有不同的行为?

    测试是否使用 org.junit.@Test 注释?

    【讨论】:

      【解决方案5】:

      如果您查看 Surefire 插件的源代码,它会做一些偷偷摸摸的事情。如果它在 Classloader 中找到任何 TestNG 包,它将选择运行 TestNG TestRunner。我还没有看到 JUNit 和 TestNG 测试并排运行良好的任何示例。

      【讨论】:

      • 这可以通过两次执行来实现。
      【解决方案6】:

      我遇到了同样的问题,我花了一段时间才弄清楚。我的设置是引入旧版本的 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>
      

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          猜你喜欢
          • 2016-02-16
          • 2014-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-03
          • 1970-01-01
          • 1970-01-01
          • 2015-12-12
          相关资源
          最近更新 更多