准备工作
导入junit的jar包的方法:新建lib目录,将两个jar包(junit-4.11.jar和hamcrest-core-1.3.jar)放入,然后,选中两个jar包,右键Add as Library
测试
安卓测试 log日志必须打开avd才能看得到,而且要运行在主程序
java测试,使用sout就能看到
例
安卓测试
package com.example.androidtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import org.junit.Test;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test test=new test();
test.testTest();
}
public class test {
private static final String TAG="test1";
public Random random = new Random();
@Test
public void testTest(){
int red = random.nextInt(256) / 1;
int green = random.nextInt(256) / 1;
int blue = random.nextInt(256) / 1;
//Toast.makeText(MainActivity.this, "red"+"\n"+red+"greed"+"\n"+green+"blue"+"\n"+blue, Toast.LENGTH_SHORT).show();
// System.out.println("red"+red+"greed"+green+"blue"+blue);
Log.d(TAG, "red"+red+"\n"+"greed"+green+"\n"+"blue"+blue);
}
}
}
java测试
a.java
package com.example.androidtest;
import android.util.Log;
import org.junit.Test;
import java.util.Random;
public class a {
private static final String TAG="test1";
public Random random = new Random();
public void testTest(){
int red = random.nextInt(256) / 1;
int green = random.nextInt(256) / 1;
int blue = random.nextInt(256) / 1;
//Toast.makeText(MainActivity.this, "red"+"\n"+red+"greed"+"\n"+green+"blue"+"\n"+blue, Toast.LENGTH_SHORT).show();
// System.out.println("red"+red+"greed"+green+"blue"+blue);
Log.d(TAG, "red"+red+"\n"+"greed"+green+"\n"+"blue"+blue);
System.out.println("red"+red+"\n"+"greed"+green+"\n"+"blue"+blue);
}
}
testa.java
package com.example.androidtest;
import org.junit.Test;
public class testa {
@Test
public void testa()
{
a a1 = new a();
a1.testTest();
}
}