views:

560

answers:

4

Hi,

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

+1  A: 

Powermock setup looks Ok to me, and the jars seem fine (assuming maven transitive dependencies get the other powermock jars - we have about 6-7 after our ivy resolve gets them)

Eclipse might be using it's own "internal" JUnit library, thus the different behaviours ?

Are the test annotated with org.junit.@Test ?

Phantomwhale
Yes, the tests are annotated with @org.junit.Test
Miguel Ping
+1  A: 

I can't reproduce your problem. With the following content in my pom.xml:

  <repositories>
    <repository>
      <id>powermock-repo</id>
      <url>http://powermock.googlecode.com/svn/repo/&lt;/url&gt;
    </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>

And the following test class (skipping the imports):

@RunWith(PowerMockRunner.class)
@PrepareForTest( { App.class })
public class AppTest {
    @Test
    public void testApp() {
        assertTrue(true);
    }
}

Running mvn test -Dtest=AppTest just works fine and give me the following output:

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

So the question is: do you have a method annotated with @Test in AlertNotificationsTest?

Pascal Thivent
Yes, I have alot of tests annotated with @org.junit.Test.
Miguel Ping
Please post your `pom.xml` then. As I said, I can't reproduce the behavior you describe. And BTW, what do you observe when you run `mvn test`?
Pascal Thivent
Have posted what you requested
Miguel Ping
A: 

There was a problem when mixing both TestNG & JUnit tests. Migrating all tests to Junit solved my problem. Thanks guys.

Miguel Ping
A: 

If you look into the source of the Surefire plugin, it does some sneaky stuff. If it finds any TestNG packages in the Classloader, it will opt to run a TestNG TestRunner. I haven't yet seen any examples of both JUNit and TestNG tests running well side-by-side.

Nelz
This is possible using two executions.
Pascal Thivent