PackageCom.itheima.junit;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } }
Packagecom.itheima.junit.test;Importcom.itheima.junit.utils.Utils;Importandroid.test.AndroidTestCase;//black box test (code not understood)//White Box Test (understand code) Public classTestCaseextendsAndroidtestcase {//#单元测试junit (test function does not start the entire project) Public voidTest () {//is not the method of rewriting, the right mouse button can run up. intresult = Utils.add (3, 5); //assertion: Used to detect whether actual values are consistent with expectations//The role of assertions, the detection of running results and the consistency of expectations//If an application has an exception, it is thrown to the test frameworkAssertequals (8, result);//8 is the expectation, and result is the actual value. } Public voidtest2 () {Utils.chuyi (2, 1); }}
package com.itheima.junit.utils; public class Utils { static int Add (int I, int J) { return i + J; public static void Chuyi (int i, int J) { int result = i/ J; }}
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.itheima.junit"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "8"android:targetsdkversion= "/>"* When specifying the instruction set, Targetpackage specifies the package name of the app you want to test<Instrumentation Android:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.itheima.junit"The package name of the project is the manifest package= "Com.itheima.junit" above.></instrumentation> <Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >* Define the class library used<uses-library android:name= "Android.test.runner"/> <Activity Android:name= "Com.itheima.junit.MainActivity"Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter& Gt </activity> </application></manifest>
Android 68 Unit Test