- Cause
- Just out of the Android studio. I wanted to get a unit test, but it was a pity that Android studio didn't know what the wind was (and, to be precise, I wouldn't do it). Unable to execute to the appropriate code.
And then today suddenly I was jerking off. Also want to go to get the Android JUnit.
- This article is based on the use of Android JUnit for Eclipse Development, if Eclipse's Android JUnit has not been used, I haven't said it!
- Prepare the environment, configure
- Website Demo Address: https://github.com/googlesamples/android-testing-templates.git
The document is: Https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support
- Environment
- According to the demo
- Unit tests that simply want to run Java introduce
Dependencies for local unit tests
Testcompile ' junit:junit: ' + rootProject.ext.junitVersion
Testcompile ' Org.mockito:mockito-all: ' + rootProject.ext.mockitoVersion
Testcompile ' Org.hamcrest:hamcrest-all: ' + rootProject.ext.hamcrestVersion
- The junit that wants to run Android is introduced
Android testing Support Library ' s runner and rules
Androidtestcompile ' Com.android.support.test:runner: ' + rootProject.ext.runnerVersion
Androidtestcompile ' com.android.support.test:rules: ' + rootProject.ext.rulesVersion
- Finally, add the Defaultconfig node
Defaultconfig {
Testinstrumentationrunner ' Android.support.test.runner.AndroidJUnitRunner '
}
- The above configuration is to be configured in the "Startup Project" Build.gradle file to automatically associate on startup projects
- Android Studio originally launched the test code in a project into two directories
- If it is by changing "sourcesets". That's going to have to be reconfigured. Specify the appropriate folder (this is the configuration exported by Eclipse transfer as)
sourcesets{
sourcesets{
main{
Java.srcdirs = [' src ']
}
androidtest{
Java.srcdirs = [' androidtest/src ']
}
test{
Java.srcdirs = [' test/src ']
}
}
- Finally, put the corresponding file into the specific directory
- Androidtest "The JUnit of Android"
- Test "unit test for Java"
- Code
- Junit
public class Exampleunittest {
@Test
public void Addition_iscorrect () throws Exception {
System.out.println ("123");
}
}
- Android JUnit
public class Androidruntimecodetest extends Androidtestcase {
public void Testhello () throws Exception {
System.out.println ("Testhello");
}
}
- Conclusion
- Precautions
- previous eclipse did unit testing to Androidmanifest.xml to indicate
<manifest ><instrumentation & Nbsp; android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= "Com.example.viewtest" &NBSP;/>
</manifest>
- <application>
<uses-library android:name= "Android.test.runner" /> ;
</application>
- Android Studio build.gradle
- Import the appropriate package
- at
A ndroid{ defaultconfig { testinstrumentationrunner ' Android.support.test.runner.AndroidJUnitRunner ' }}
- One more thing: "The required package for test use" encounters a conflict with the "Package for Startup Project", using the
Reference: https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/user-guide# toc-resolving-conflicts-between-main-and-test-apk
Configurations.all {
Resolutionstrategy {
Androidtestcompile ' com.android.support.test:runner:0.4.1 '
Androidtestcompile ' com.android.support.test:rules:0.4.1 '
Forcedmodules = [' com.android.support:support-annotations:23.0.1 ']
}
}
"Simple Configuration" unit test in Android Studio (Android Junit)