我试图将 PowerMock 与 Mockito 一起使用,但 PowerMock.replayAll(); 和 PowerMock.verifyAll(); 在我的 Eclipse 环境中找不到。使用了这个下载链接:
http://code.google.com/p/powermock/downloads/detail?name=powermock-mockito-junit-1.5.zip&can=2&q=
并在此处下载 EasyMock:
http://sourceforge.net/projects/easymock/files/EasyMock/3.1/easymock-3.1.zip/download
将所有 jar 添加到我的 libs 目录(从我的构建路径中删除它)。任何人?谢谢!
源码Android项目和测试项目:
https://github.com/powder366/test
https://github.com/powder366/testtest
例子。
package com.test
import static org.junit.Assert.*;
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.PowerMockUtils;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Greeter.class)
public class MockStaticExampleTest {
@Test
public void mockStaticExample() throws Exception {
String expectedGreeting = "greeting";
String nameToGreet = "name";
PowerMockito.mockStatic(Greeter.class);
EasyMock.expect(Greeter.getGreeting(nameToGreet)).andReturn(expectedGreeting);
PowerMock.replayAll();
String actualGreeting = Greeter.getGreeting(nameToGreet);
PowerMock.verifyAll();
assertEquals("Expected and actual greeting did not match", expectedGreeting, actualGreeting);
}
}