0

我在使用 maven 的 powermock 和 mockito 时遇到了一个奇怪的构建问题。我可以用 ecplise 完美地运行测试(不使用 maven)。但是当我尝试从 cli 或在构建服务器上使用 maven 运行测试时,我得到以下异常:

错误测试:测试机制:java.lang.ClassNotFoundException:org.mockito.internal.progres s.ThreadSafeMockingProgress

我的单元测试如下所示:

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()));
    }
}

我的 pom.xml 看起来像这样:

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

从 CLI 构建时,我收到以下错误:

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

任何人都知道为什么我会收到此错误?非常感谢您的帮助...

4

3 回答 3

1

您需要像这样将 Dexmaker 添加到您的依赖项中:

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

但是,我认为它不适用于 Powermock。这篇文章可以帮助你。

于 2012-12-10T15:46:51.850 回答
0

您的 Maven 配置看起来是正确的,除了 org.mockito.internal.progress.ThreadSafeMockingProgress,不应该丢失。

执行以下命令时,您看到的是哪个 mockito 版本?

mvn dependency:build-classpath -DincludeScope=test
于 2011-10-25T16:54:53.990 回答
0

以防万一这随机帮助某人,我通过注释掉一个 Surefire 插件配置来克服同样的错误。

我有同样的错误,分析了 Eclipse/Maven 类路径,发现没有区别(除了“/eclipse/configuration/org.eclipse.osgi/bundles/320/1/.cp/”条目)。

就我而言,我之前在 POM 中禁用了 Surefire 插件的测试分叉(以解决不同的问题)。我想我会尝试将其评论出来,然后让它再次工作。随机的,但我想我会传递它。

为了完整起见,我注释掉的配置如下:

<!--
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.7.2</version>
            <configuration>
                <forkMode>never</forkMode>
            </configuration>
        </plugin>
-->
于 2011-10-31T18:27:52.003 回答