为什么进行单元测试
在我们开发的app的时候,可能会出现一些逻辑问题是测试人员测试不到的,或者在测试前需要自测的时候,希望程序自动执行,这时候就需要使用单元测试的。使用单元测试,就会需要模拟一些类或者变量,这时我们就需要使用PowerMock。
使用PowerMock
我们新建一个Android Studio工程,默认会在工程下生成如下三个文件夹:androidTest, main,test。main不用说了就是你的程序入口,androidTest是android特有的测试方法,这个后面会讲。这里主要说一下test,这个是测试java的相关程序,理论上任何java都可以利用这个进行测试。android也是java写的,当然就也可以进行测试。
依赖
我们首先需要让工程依赖测试的相关库。
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详解
开始测试
基本数据传递
我们在MainActivity中写一个方法,来判断文件是否存在:
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,或者新建一个类都可以,添加如下方法:
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));//断言,这个不用说了吧
}
}
然后右键点击该文件,选择运行:
可以看到运行成功,一切都是正确的。根据注释可以明白我们的意图,就是当
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();
}
测试代码如下:
@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对象代码所在的类。
模拟一个类中的final类型方法
首先我们需要写一个包含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());
}
spy
当我们想检测一个类中变量的变化时可以用这种方式:
比如我们写一个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()
可能出现的问题
- org.powermock.reflect.exceptions.FieldNotFoundException:
junit和powermock版本冲突:
可以做如下修改:
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"
}
- 使用Powermock后会提示classloader错误
加入注解:@PowerMockIgnore("javax.management.*")