Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Junit & PowerMock with Mockito through PowerMockRunner from maven

I am not being able to run Powermock through maven. I'm the PowerMock Mockito and PowerMockRunner for driving a jUnit test.

Here's the test:

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

I haven't configured anyting special for running the test. My pom references the following deps:

  • 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

when I run mvn -Dtest=AlertNotificationsTest test mvn says there's no test to run. But if I run the same test class from eclipse, everything runs ok.

Am I doing something wrong?


Here's my pom.xml below (the relevant parts)

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

Here's the output from maven

mvn -Dtest=AlertNotificationsTest test

...
[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] ------------------------------------------------------------------------

Note: I can run other tests, I just can't run this test. If I make the AlertNotificationsTest class extend junit.framework.TestCase the class gets picked up by maven, but it seems that it does not get driven by PowerMockRunner.

Here's the output of that:


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

Again, these tests run just fine with Eclipse.


Update I found a possible problem & workaround. I have tests with TestNG and JUnit. If I remove TestNG from my pom and migrate all my tests to JUnit, I am able to run my PowerMock test with mvn test. So it seems that there's a problem with maven and the junit/testng combo.

I'd like to be able to run both, but If I don't find a way I'll go and answer my own question. Thanks guys&gals

like image 383
Miguel Ping Avatar asked Nov 25 '09 13:11

Miguel Ping


Video Answer


1 Answers

I just had this error and worked through the solution. My pom.xml file had the following dependency:

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-mockito-release-full</artifactId>
  <version>1.5</version>
  <classifier>full</classifier>
  <scope>test</scope>
</dependency>

The problem comes from the fact my code uses JUnit and the above dependency has an external dependency on TestNG. This was stopping my test from running. Why I don't know - you would have though a test framework would have been tested a little bit better!!!

Anyway the solution was to break down the 'full' dependencies to just those required:

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

That solved it. BTW I used mvn dependency:tree to understand the associated dependencies.

like image 65
Adam Davies Avatar answered Oct 25 '22 16:10

Adam Davies