在Android中使用junit测试时,
在local test中,
不需要测试activity,但是类里使用到intent的时候,可以这样子取得intent
项目的junit,robolectric,请自行设定。
@RunWith(RobolectricTestRunner.class)
public class SteraPaymentUtilTest {
@Test
public void test01() {
//如此定义,即可取得intent
Intent intent = new Intent(Robolectric.application.getApplicationContext(), Activity.class);
}
}
另外在显示测试结果的时候,有时候会有乱码
只需要在文件(C:\Users\用户名\.AndroidStudio4.0\config\studio64.exe.vmoptions)里加上编码设定即可。
编码设定追加方法,选择菜单「Help>Edit Custom VM Options..」
会打开以下文件,如果文件不存在会自动创建。
粘贴【-Dfile.encoding=UTF-8】即可
重启Android studio之后,显示正常了。