"Simple Configuration" unit test in Android Studio (Android Junit)

Source: Internet
Author: User

  1. Cause
      1. 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.
      2. 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!
  2. Prepare the environment, configure
    1. 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
    2. Environment
      1. According to the demo
          1. 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
          2. 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
      2. Finally, add the Defaultconfig node


        Defaultconfig {
        Testinstrumentationrunner ' Android.support.test.runner.AndroidJUnitRunner '
        }


      3. The above configuration is to be configured in the "Startup Project" Build.gradle file to automatically associate on startup projects

          1. Android Studio originally launched the test code in a project into two directories
          2. 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 ']
            }
            }
          3. Finally, put the corresponding file into the specific directory
            1. Androidtest "The JUnit of Android"
            2. Test "unit test for Java"
  3. Code
      1. Junit
        public class Exampleunittest {
        @Test
        public void Addition_iscorrect () throws Exception {
        System.out.println ("123");

        }
        }
      2. Android JUnit
        public class Androidruntimecodetest extends Androidtestcase {

        public void Testhello () throws Exception {
        System.out.println ("Testhello");
        }
        }
  4. Conclusion
    1. Precautions
        1. previous eclipse did unit testing to Androidmanifest.xml to indicate

          1. <manifest ><instrumentation  & Nbsp;     android:name= "Android.test.InstrumentationTestRunner"          android:targetpackage= "Com.example.viewtest" &NBSP;/>
            </manifest>
          2. <application>
            <uses-library android:name= "Android.test.runner"  /> ;
            </application>
        2. Android Studio build.gradle
          1. Import the appropriate package
          2. at
            A ndroid{    defaultconfig {         testinstrumentationrunner  ' Android.support.test.runner.AndroidJUnitRunner '     }}
    2. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.