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

Mig*_*ing 9 java junit maven-2 mockito powermock

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

这是测试:

@RunWith(PowerMockRunner.class)
@PrepareForTest( { UserLocalServiceUtil.class, ExpandoBridge.class })
public class AlertNotificationsTest {
//...
Run Code Online (Sandbox Code Playgroud)

我没有为运行测试配置任何特殊功能.我的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 testmvn时说没有测试可以运行.但如果我从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>
Run Code Online (Sandbox Code Playgroud)

这是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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

注意:我可以运行其他测试,我只是无法运行此测试.如果我让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
Run Code Online (Sandbox Code Playgroud)

同样,这些测试在Eclipse中运行得很好.


更新我发现了一个可能的问题和解决方法.我有TestNG和JUnit的测试.如果我从我的pom中删除TestNG并将所有测试迁移到JUnit,我可以运行我的PowerMock测试mvn test.所以似乎maven和junit/testng组合存在问题.

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

Ada*_*ies 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>
Run Code Online (Sandbox Code Playgroud)

问题来自于我的代码使用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>
Run Code Online (Sandbox Code Playgroud)

这解决了它.顺便说一下,我曾经mvn dependency:tree了解相关的依赖关系.


Mig*_*ing -1

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