1. Build a new Android project
Package Com.shellway.junit; Public class Service { publicint divide (int A,int b) { Return A/b; }}
Service.java. As the class being tested
PackageCom.shellway.junit;ImportJunit.framework.Assert;Importandroid.test.AndroidTestCase; Public classTesttextendsAndroidtestcase { Public voidtest1 () {Service service=NewService (); System.out.println (Service.divide (10, 2)); } Public voidtest2 () {Service service=NewService (); System.out.println (Service.divide (10, 0)); } Public voidtest3 () {Service service=NewService (); Assert.assertequals (2.5, Service.divide (10, 4)); }}
Testt inherits Androidtestcase, as a test class
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.shellway.junit"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" /> <InstrumentationAndroid:targetpackage= "Com.shellway.junit"Android:name= "Android.test.InstrumentationTestRunner" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <uses-libraryAndroid:name= "Android.test.runner" /> <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
Androidmanifest.xml
Note: The content to be included in the configuration file is:
< Instrumentation Android:targetpackage = "Com.shellway.junit" android:name= "Android.test.InstrumentationTestRunner"/>< android:name= "Android.test.runner"/>
View Code
and pay attention to where they are joining. This is a way to test your own engineering.
Another way is to create a new Android JUnit project to test another project, it is important to note that when you build this test project name can not be named by test, or the error will be difficult to find out the problem, the following is a test to name the project error message.
Create a new test project with the JUnit that you carry in Android