在咱们开发的app的时候,可能会出现一些逻辑问题是测试人员测试不到的,或者在测试前须要自测的时候,但愿程序自动执行,这时候就须要使用单元测试的。使用单元测试,就会须要模拟一些类或者变量,这时咱们就须要使用PowerMock。javascript
咱们新建一个Android Studio工程,默认会在工程下生成以下三个文件夹:androidTest, main,test。main不用说了就是你的程序入口,androidTest是android特有的测试方法,这个后面会讲。这里主要说一下test,这个是测试java的相关程序,理论上任何java均可以利用这个进行测试。android也是java写的,固然就也能够进行测试。java
咱们首先须要让工程依赖测试的相关库。android
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile "org.mockito:mockito-all:1.6.1"
testCompile 'org.powermock:powermock-module-junit4:1.6.1'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1'
testCompile 'org.powermock:powermock-api-mockito:1.6.1'
testCompile "org.powermock:powermock-classloading-xstream:1.6.1"
}复制代码
为何用testCompile,这是代表只有在运行test工程时,才会依赖这些。若是对这里的语法有什么疑问,能够参考我以前的文章Android工程gradle详解api
咱们在MainActivity中写一个方法,来判断文件是否存在:app
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean checkFile(File file){
return file.exists();
}
}复制代码
而后在上文中提到过的test文件夹下打开ExampleUnitTest,或者新建一个类均可以,添加以下方法:ide
public class ExampleUnitTest {
@Test
public void testEmpty() {
File file = PowerMockito.mock(File.class);//模拟一个file文件
MainActivity activity = new MainActivity();//新建一个这个类
PowerMockito.when(file.exists()).thenReturn(true);//因为file是模拟变量,那么就要指定当调用模拟变量的某个方法时所返回的值
Assert.assertTrue(activity.checkFile(file));//断言,这个不用说了吧
}
}复制代码
而后右键点击该文件,选择运行:post
file.exists()
返回true时,那么这个方法也要返回true,我就认唔这个方法没毛病。那若是方法出错是什么样呢
public boolean checkFile(File file){
return !file.exists();//故意写错
}复制代码
看看测试代码能不能检测出来,运行:单元测试
上面测试的对象是传递进来的,那么咱们若是测试一个new的对象能够吗?固然也能够:
代码以下:测试
public boolean checkFileByPath(String path){
File file = new File(path);
return file.exists();
}复制代码
测试代码以下:gradle
@RunWith(PowerMockRunner.class)
public class ExampleUnitTest {
@Test
public void testEmpty() {
File file = PowerMockito.mock(File.class);
MainActivity activity = new MainActivity();
PowerMockito.when(file.exists()).thenReturn(true);
Assert.assertTrue(activity.checkFile(file));
}
@Test
@PrepareForTest(MainActivity.class)
public void testEmptyBypath() throws Exception {
File file = PowerMockito.mock(File.class);
MainActivity activity = new MainActivity();
PowerMockito.whenNew(File.class).withArguments("path").thenReturn(file);//意思是当new File时返回模拟变量file
PowerMockito.when(file.exists()).thenReturn(true);
Assert.assertTrue(activity.checkFileByPath("path"));//传入的变量与上面构建时一致,都要是path
}
}复制代码
注意当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是须要mock的new对象代码所在的类。
首先咱们须要写一个包含fina方法的类:
public class StringUtil {
public final boolean isOk(){
return true;
}
}复制代码
而后调用:
public boolean checkString(StringUtil util){
return util.isOk();
}复制代码
接着给出测试代码:
@Test
@PrepareForTest(StringUtil.class)
public void testFinal() throws Exception {
StringUtil util = PowerMockito.mock(StringUtil.class);
MainActivity activity = new MainActivity();
PowerMockito.when(util.isOk()).thenReturn(true);
Assert.assertTrue(activity.checkString(util));
}复制代码
当须要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
给出静态方法:
public class StringUtil {
public final boolean isOk(){
return true;
}
public static boolean isOkStatic(){
return true;
}
}复制代码
给出调用:
public boolean checkStringByStatic(){
return StringUtil.isOkStatic();
}复制代码
测试代码:
@Test
@PrepareForTest(StringUtil.class)
public void testStatic() throws Exception {
MainActivity activity = new MainActivity();
PowerMockito.mockStatic(StringUtil.class);
PowerMockito.when(StringUtil.isOkStatic()).thenReturn(true);
Assert.assertTrue(activity.checkStringByStatic());
}复制代码
当须要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。
咱们写一个私有方法和调用:
public boolean checkStringPrivate(){
return isOkPrivate();
}
private boolean isOkPrivate(){
return true;
}复制代码
测试代码:
@Test
@PrepareForTest(MainActivity.class)
public void testPrivate() throws Exception {
MainActivity mainActivity = PowerMockito.mock(MainActivity.class);
PowerMockito.when(mainActivity.checkStringPrivate()).thenCallRealMethod();
PowerMockito.when(mainActivity, "isOkPrivate").thenReturn(true);//isOkPrivate是私有方法,可是模拟了MainActivity,能够经过公有方法间接调用测试
Assert.assertTrue(mainActivity.checkStringPrivate());
}复制代码
当咱们想检测一个类中变量的变化时能够用这种方式:
好比咱们写一个Spyer类:
public class Spyer {
public int count = 0;
public void onCreate(){
count = 1;
}
}复制代码
咱们在测试的时候能够这样:
@Test
public void testSpy() throws Exception {
Spyer spyer = PowerMockito.spy(new Spyer());
spyer.onCreate();
Assert.assertEquals(1, spyer.count);
}复制代码
当咱们想测试一个方法时,可能不想传入某一个精确的值,这时候可使用Mockito.anyInt(),Mockito.anyString()
##
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile "org.mockito:mockito-all:1.6.1"
testCompile 'org.powermock:powermock-module-junit4:1.6.1'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1'
testCompile 'org.powermock:powermock-api-mockito:1.6.1'
testCompile "org.powermock:powermock-classloading-xstream:1.6.1"
}复制代码
@PowerMockIgnore("javax.management.*")