我正在用powermock和mockito使用maven面臨一個奇怪的生成問題。我可以完美地運行ecplise測試(不使用maven)。但是,當我嘗試從CLI或構建服務器我得到下面的異常運行與Maven測試:Android + Powermock + Mockito + Maven用ClassNotFoundException生成錯誤
測試錯誤: 測試機制:拋出java.lang.ClassNotFoundException:org.mockito.internal.progres 小號.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
任何人都有一個想法,爲什麼我得到這個錯誤?非常感謝幫助...
謝謝您的回答,但 MVN依賴:內置的classpath給我下面的神器的Mockito: .m2目錄\庫\組織\的Mockito \的Mockito,所有\ 1.8.5 \的Mockito-ALL-1.8.5 .jar – onlythoughtworks
我無法在那裏查明問題,您應該嘗試在Maven構建中進行簡單的測試,然後添加一些功能/依賴項,直到測試中斷。 – Brice
是的,我必須去那條路線,而我忘了提及的是,測試實際上運行通過,我可以看到在控制檯委託日誌輸出,所有11個測試運行,但總是這一個錯誤... – onlythoughtworks