English 中文(简体)
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:DevelJavaEP_PORTALinformation-provider	argetsurefi

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

最佳回答

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

问题回答

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.

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

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?

I came across this issue as well, but its not a PowerMock issue. My Test class was named XStaticTests.java.

When I run "mvn clean test", this test would not run, it only ran when I specified the test using "-Dtest=..."

The surefire documentation mentions that by default only these patterns are searched for : "/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test". "/Test.java" - includes all of its subdirectories and all java filenames that end with "Test". "*/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".

Therefore changing the classname to one that ends with one of these, will run when "mvn test" is called, else the surefire plugin needs to be configured with the class name specifically.

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 ?

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.

I had the same problem, and it took me a while to figure out. My setup was pulling in an older version of jboss.javassist, which oddly was preventing the PowerMockRunner from working at all.

It s worth noting that I also have a mixed JUnit / TestNG environment. I previously tried the solution of adding multiple surefire providers, and that didn t work either (using surefire 2.14.1). After upgrading to surefire 2.17, both my JUnit and TestNG tests started running without needing to declare any surefire providers.

Here s my plugin section...

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

... and the relevant testing deps ...

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签