繁体   English   中英

通过Maven的PowerMockRunner与Mockito一起运行Junit和PowerMock

[英]Running Junit & PowerMock with Mockito through PowerMockRunner from maven

我无法通过maven运行Powermock。 我是用于驱动jUnit测试的PowerMock Mockito和PowerMockRunner。

这是测试:

@RunWith(PowerMockRunner.class)
@PrepareForTest( { UserLocalServiceUtil.class, ExpandoBridge.class })
public class AlertNotificationsTest {
//...

我没有为运行测试配置任何特殊功能。 我的pom引用了以下代码:

  • 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

当我运行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 mvn test 所以似乎maven和junit / testng组合存在问题。

我希望能够同时运行,但如果我找不到办法,我会去回答我自己的问题。 谢谢你们和gals

我刚刚遇到这个错误,并通过解决方案。 我的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>

这解决了它。 BTW我用mvn dependency:tree来理解相关的依赖。

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

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

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

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

我无法重现你的问题。 在我的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注释的方法吗?

Powermock设置对我来说很好看,并且罐子看起来很好(假设maven传递依赖性得到了其他的powermock罐子 - 我们在常春藤解析后获得它们大约6-7)

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

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

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

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

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

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

如果你查看Surefire插件的来源,它会做一些偷偷摸摸的东西。 如果它在类加载器中找到任何TestNG包,它将选择运行TestNG TestRunner。 我还没有看到任何JUNit和TestNG测试并行运行的例子。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM