android studio 单元测试 JUnit
2016-12-29 10:24
232 查看
环境准备
在 .gradle中配置
defaultConfig {
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
}
dependencies {
testCompile ‘junit:junit:4.12’
compile ‘com.android.support.test.espresso:espresso-core:2.2.2’
compile ‘com.android.support.test:runner:0.5’
}
纯Java测试
1、测试类
public class JavaTextActivity {
private int date;
public void JavaTextActivity(){
}
public String sayHello(int now , String user){
date = now;
String tall = “Hi,”+user+”.”+getGreeting();
return tall;
}
}
2.测试步骤
选择 JavaTextActivity 类名 —> 右键 Go To —->Test —->Create New Test —–>选中 setUp@Before And tearDown@After ——> OK ——->生成测试类 JavaTextActivityTest
3.测试类
public class JavaTextActivityTest {
private JavaTextActivity javaTextActivity = null;
@Before
public void setUp() throws Exception {
javaTextActivity = new JavaTextActivity();
}
}
4.运行测试类
在工程列表中选中 JavaTextActivityTest ——–>右键 Run “JavaTextActivityTest ”
5.测试类中的方法
在Favorite 窗口中选中 JavaTextActivityTest 中的 testSayHelloInTheMorning()右键选中
Run
6.测试结果
assertEquals(result,”Hi,煲约二.Good morning!”);
if (result == “Hi,煲约二.Good morning!”)
正确结果 Process finished with exit code 0
else
错误结果
在 .gradle中配置
defaultConfig {
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
}
dependencies {
testCompile ‘junit:junit:4.12’
compile ‘com.android.support.test.espresso:espresso-core:2.2.2’
compile ‘com.android.support.test:runner:0.5’
}
纯Java测试
1、测试类
public class JavaTextActivity {
private int date;
public void JavaTextActivity(){
}
public String sayHello(int now , String user){
date = now;
String tall = “Hi,”+user+”.”+getGreeting();
return tall;
}
public String getGreeting() { if(date == 1) return "Happy new year!"; else if(date >1 && date<6) return "Good morning!"; else if(date==6) return "Good afternoon!"; else return "Good night!"; }
}
2.测试步骤
选择 JavaTextActivity 类名 —> 右键 Go To —->Test —->Create New Test —–>选中 setUp@Before And tearDown@After ——> OK ——->生成测试类 JavaTextActivityTest
3.测试类
public class JavaTextActivityTest {
private JavaTextActivity javaTextActivity = null;
@Before
public void setUp() throws Exception {
javaTextActivity = new JavaTextActivity();
}
@After public void tearDown() throws Exception { javaTextActivity = null; } @Test public void sayHello() throws Exception { } @Test public void testSayHelloInTheMorning() throws Exception{ int date = 2; String user = "煲约二"; String result = javaTextActivity.sayHello(date,user); assertEquals(result,"Hi,煲约二.Good morning!"); }
}
4.运行测试类
在工程列表中选中 JavaTextActivityTest ——–>右键 Run “JavaTextActivityTest ”
5.测试类中的方法
在Favorite 窗口中选中 JavaTextActivityTest 中的 testSayHelloInTheMorning()右键选中
Run
6.测试结果
assertEquals(result,”Hi,煲约二.Good morning!”);
if (result == “Hi,煲约二.Good morning!”)
正确结果 Process finished with exit code 0
else
错误结果
相关文章推荐
- Android Studio中Junit单元测试使用JSON对象异常的问题
- Android Studio中使用junit做单元测试
- Android Studio 1.5使用junit单元测试,以及“Test running startedTest running failed: Instrumentation run failed
- 在Android Studio进行“简单配置”单元测试(Android Junit)
- Android studio单元测试(JUnit)
- Android Studio下JUnit单元测试
- Ant和JUnit结合进行单元测试 ant 软件包 org.junit 不存在
- Java单元测试(Junit+Mock+代码覆盖率)
- Android Studio 单元测试(instrumentation)
- JUnit 单元测试基本用法
- Java单元测试工具:JUnit4(四)——JUnit测试套件使用及参数化设置
- java单元测试(Junit)
- Junit(3)JUnit和单元测试入门简介
- junit单元测试(重要)
- Java____Eclipse下JUnit单元测试(初级)
- JUnit单元测试(一)--JUnit简介
- Junit单元测试Spring 读取WEB-INF下的配置文件
- Java单元测试(Junit+Mock+代码覆盖率)
- springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试
- Junit 单元测试 Spring 读取WEB-INF下 xml 文件