0

I´m facing a strange build problem with powermock and mockito using maven. I can perfectly run the tests wihtin ecplise (wihtout using maven). But when i try to run the test with maven from cli or on the build server i get the following exception:

Tests in error: Test mechanism: java.lang.ClassNotFoundException: org.mockito.internal.progres s.ThreadSafeMockingProgress

My unit test looks like this:

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import android.util.Log;

import com.generic_io.concurrent.ResultReceiverCallableFactory;


@RunWith(PowerMockRunner.class)
@PrepareForTest({
    Log.class
})
public class TestPowerMockTest
{

    @Mock
    ResultReceiverCallableFactory callableFactory;

    @Before
    public void setUp() throws IOException
    {
        mockLogger();
    }


    private void mockLogger()
    {
        PowerMockito.mockStatic(Log.class);
        when(Log.d(anyString(), anyString())).thenAnswer(new Answer<Integer>()
        {

            @Override
            public Integer answer(final InvocationOnMock invocation) throws Throwable
            {
                final String tag = (String) invocation.getArguments()[0];
                final String msg = (String) invocation.getArguments()[1];
                System.out.println("[" + tag + "] " + msg);
                return 0;
            }
        });

    }


    @Test
    public void testPreconditions()
    {
        assertThat("ResultReceiverCallableFactory is null", callableFactory, is(notNullValue()));
    }
}

My pom.xml looks like this:

<dependencies>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
      <version>2.2.1</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.generic_io</groupId>
      <artifactId>generic_io</artifactId>
      <version>1.0-SNAPSHOT</version>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.pivotallabs</groupId>
      <artifactId>robolectric</artifactId>
      <version>1.0-RC1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <type>jar</type>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito</artifactId>
      <version>1.4.9</version>
      <type>jar</type>
       <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>1.4.9</version>
      <type>jar</type>
       <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.5</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>

     <dependency>
      <groupId>net.sourceforge.cobertura</groupId>
      <artifactId>cobertura</artifactId>
      <version>1.9.4.1</version>
    </dependency>

  </dependencies>

When building from CLI i get the following error:

Tests in error:
  Test mechanism: java.lang.ClassNotFoundException: org.mockito.internal.progres
s.ThreadSafeMockingProgress

Anyone has an idea why i get this error? Help is very much appreciated...

onlythoughtworks
  • 794
  • 8
  • 13

3 Answers3

1

You need to add Dexmaker to your dependencies like this :

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
    </dependency>
    <dependency>
        <groupId>com.google.dexmaker</groupId>
        <artifactId>dexmaker</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.dexmaker</groupId>
        <artifactId>dexmaker-mockito</artifactId>
        <version>1.0</version>
    </dependency>

However, I think it not works with Powermock. This post can help you.

Mickael M.
  • 197
  • 3
  • 4
0

Your maven configuration looks correct, besides org.mockito.internal.progress.ThreadSafeMockingProgress, shouldn't be missing.

Which mockito version are you seeing when doing the following command ?

mvn dependency:build-classpath -DincludeScope=test
bric3
  • 40,072
  • 9
  • 91
  • 111
  • Thanks for your answer but the mvn dependency:build-classpath gives me the following artifact for mockito: .m2\repository\org\mockito\mockito-all\1.8.5\mockito-all-1.8.5.jar – onlythoughtworks Oct 25 '11 at 17:20
  • I cannot pinpoint the issue there, you should try to make a simple test working in the maven build, then add a few feature / dependencies until the test breaks. – bric3 Oct 25 '11 at 20:14
  • Yes i have to go that route, and what i forgot to mention is that the test actually runs through, i can see the delegated log output in the console, all 11 tests are run but, but always this one error... – onlythoughtworks Oct 26 '11 at 07:17
  • Maybe a tip, I see you have cobertura here, if you have a reporting section in your pom, try removing it. – bric3 Oct 26 '11 at 17:29
0

Just in case this randomly helps someone, I got past the same error by commenting out a Surefire plugin configuration.

I had the same error, analysed Eclipse/Maven classpaths, found no difference (apart from "/eclipse/configuration/org.eclipse.osgi/bundles/320/1/.cp/" entries).

In my case, I had previously disabled test forking for the Surefire plugin in the POM (to troubleshoot a different issue). I thought I'd just try commenting that out and that got it working again. Random, but I thought I'd pass it on.

For completeness, the configuration I commented out was as follows:

<!--
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.7.2</version>
            <configuration>
                <forkMode>never</forkMode>
            </configuration>
        </plugin>
-->
David Carboni
  • 1,556
  • 23
  • 24