代码之家  ›  专栏  ›  技术社区  ›  Miguel Ping

通过maven的PowerMockRunner使用Mockito运行Junit和PowerMock

  •  8
  • Miguel Ping  · 技术社区  · 15 年前

    我无法通过maven运行Powermock。我是PowerMock Mockito和PowerMockRunner,负责驾驶jUnit测试。

    以下是测试:

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

    • org.mockito | mockito all | 1.8.0
    • junit | junit | 4.6.0
    • org.powermock.modules | powermock-module-junit4 | 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=警报通知测试

    ...
    [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 . 因此,maven和junit/testng组合似乎存在问题。

    我希望两者都能跑,但如果我找不到办法,我会去回答我自己的问题。

    7 回复  |  直到 15 年前
        1
  •  12
  •   Adam Davies    12 年前

    我只是犯了这个错误,然后解决了这个问题。我的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 了解相关的依赖关系。

        2
  •  2
  •   Pascal Thivent    15 年前

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

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

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

    @Test 在里面 AlertNotificationsTest ?

        3
  •  2
  •   shark8me    12 年前

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

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

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

        4
  •  1
  •   Phantomwhale    15 年前

    Powermock安装在我看来还不错,JAR看起来也不错(假设maven可传递依赖项得到了其他Powermock JAR——在ivy Resolution得到它们之后,我们大约有6-7个)

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

        5
  •  0
  •   Nelz    14 年前

        6
  •  0
  •   Jeff Fairley    10 年前

    值得注意的是,我还有一个混合的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
  •  -1
  •   Miguel Ping    15 年前

    混合TestNG和;JUnit测试。将所有测试迁移到Junit解决了我的问题。