9

PowermockをMavenで実行できません。私は、jUnitテストを実行するためのPowerMockMockitoおよびPowerMockRunnerです。

テストは次のとおりです。

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

テストを実行するための特別な設定はしていません。私のpomは次のdepsを参照しています:

  • org.mockito | mockito-すべて| 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を実行するとmvn -Dtest=AlertNotificationsTest test、実行するテストはないと表示されます。しかし、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に移行すると、でPowerMockテストを実行できますmvn test。したがって、Mavenとjunit/testngコンボに問題があるようです。

両方を実行できるようにしたいのですが、方法が見つからない場合は、自分の質問に答えます。ありがとうみんな&ギャルズ

4

7 に答える 7

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は関連する依存関係を理解し​​ていました。

于 2013-01-02T17:34:06.237 に答える
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は正常に機能し、次の出力が得られます。

..。
-------------------------------------------------- -----
 テスト
-------------------------------------------------- -----
com.mycompany.app.AppTestを実行しています
テストの実行:1、失敗:0、エラー:0、スキップ:0、経過時間:0.135秒

結果 :

実行されるテスト:1、失敗:0、エラー:0、スキップされた:0

[情報]  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - - -------------------------
[情報]成功を収める
[情報]  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - - -------------------------
[情報]合計時間:3秒
[情報]終了日:2009年11月25日水曜日17:34:32 CET
[情報]最終メモリ:9M / 79M
[情報]  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - - -------------------------

したがって、問題は次のとおりです。で注釈が付けられたメソッドはあります@TestAlertNotificationsTest

于 2009-11-25T16:40:04.013 に答える
2

私もこの問題に遭遇しましたが、PowerMockの問題ではありません。私のテストクラスはXStaticTests.javaという名前でした。

「mvncleantest」を実行すると、このテストは実行されず、「-Dtest=...」を使用してテストを指定した場合にのみ実行されました。

surefireのドキュメントには、デフォルトでは次のパターンのみが検索されると記載されています。「/Test*.java」-すべてのサブディレクトリと「Test」で始まるすべてのJavaファイル名が含まれます。「 /Test.java」-「Test 」で終わるすべてのサブディレクトリとすべてのJavaファイル名が含まれます。「 */*TestCase.java」-すべてのサブディレクトリと「TestCase」で終わるすべてのJavaファイル名が含まれます。

したがって、クラス名をこれらのいずれかで終わるものに変更すると、「mvn test」が呼び出されたときに実行されます。それ以外の場合は、surefireプラグインをクラス名で具体的に構成する必要があります。

于 2012-09-14T12:48:14.947 に答える
1

Powermockのセットアップは私には問題ないように見え、jarは正常に見えます(Mavenの推移的な依存関係が他のpowermock jarを取得すると仮定します-ツタの解決がそれらを取得した後、約6〜7個あります)

Eclipseは独自の「内部」JUnitライブラリを使用している可能性があるため、動作が異なりますか?

テストにはorg.junit。@Testという注釈が付けられていますか?

于 2009-11-25T13:50:38.457 に答える
0

Surefireプラグインのソースを調べると、それはいくつかの卑劣なことをします。クラスローダーでTestNGパッケージが見つかった場合は、TestNGTestRunnerを実行することを選択します。JUNitテストとTestNGテストの両方がうまく並行して実行されている例はまだ見ていません。

于 2010-10-04T23:51:41.310 に答える
0

私も同じ問題を抱えていて、理解するのに少し時間がかかりました。私のセットアップは古いバージョンのjboss.javassistをプルしていたため、奇妙なことにPowerMockRunnerがまったく機能していませんでした。

JUnitとTestNGが混在する環境もあることは注目に値します。私は以前に複数のsurefireプロバイダーを追加するソリューションを試しましたが、それも機能しませんでした(surefire 2.14.1を使用)。surefire 2.17にアップグレードした後、surefireプロバイダーを宣言する必要なしに、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>
于 2014-03-20T16:04:31.710 に答える
-1

TestNGテストとJUnitテストの両方を混在させるときに問題が発生しました。すべてのテストをJunitに移行することで、私の問題は解決しました。みんなありがとう。

于 2009-12-02T12:25:35.500 に答える