Android JUnit Test Framework

Source: Internet
Author: User
<span id="Label3"></p><p><p>To unit test an application:</p></p><p><p>Using the JUnit test framework is a must-have technology for regular Android Development. Components can be obtained in junit to simulate the correctness of sending events and detecting program Processing.</p></p><p><p>1. Configure the instruction set and function library:</p></p><p><p>(1) Configuring the instruction set, specifying the application to be tested</p></p><p><p>You need to add Instrumentationtestrunner to the instrumentation in Androidmanifest.xml and specify the package name to Test.</p></p><p><p>Code is added in Androidmanifest.xml:</p></p><p><p><instrumentation android:targetpackage= "com.example.firstdemo" android:name= " Android.test.InstrumentationTestRunner "></instrumentation></p></p><p><p>(2) Configuration Function Library</p></p><p><p>Add <uses-library android:name= "android.test.runner"/> in application</p></p><p><p>(3) adding Junit's jar Package</p></p><p><p>Project Right-click-->build Path-->add library-->junit-->junit4--> OK</p></p><p><p>2. Write the unit test code (select the method name to test, right-click "Run as" ... "Android Junit Test")</p></p><p><p>Example Code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Testpersondb<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">androidtestcase{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">testcreatedb () {personsqliteopenhelper Helper</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Personsqliteopenhelper (getcontext ()); Sqlitedatabase DB</span></span>=<span style="color: #000000;"><span style="color: #000000;">Helper.getwritabledatabase (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">testadd () {PersonDao2 DAO</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">PersonDao2 (getcontext ());</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">dao.add ("wangwu", "123",);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">dao.add ("zhangsan", "321", "a");</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span>num=8900000000000l<span style="color: #000000;"><span style="color: #000000;">; Random Random</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Random (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; I < 50; i++<span style="color: #000000;"><span style="color: #000000;">) {dao.add (</span></span>"wangwu" +i, num+i+ "", random.nextint (5000<span style="color: #000000;"><span style="color: #000000;">)); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">testfind () {PersonDao2 DAO</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">PersonDao2 (getcontext ()); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>Result=dao.find ("wangwu"<span style="color: #000000;"><span style="color: #000000;">); Assertequals (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span>, result);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Assertion: The expected value is true, the true value is the result</span></span><span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">update () {PersonDao2 DAO</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">PersonDao2 (getcontext ()); Dao.update (</span></span>"wangwu", "321"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">delete () {PersonDao2 DAO</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">PersonDao2 (getcontext ()); Dao.delete (</span></span>"wangwu"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">findAll () {PersonDao2 DAO</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">PersonDao2 (getcontext ()); List</span></span><Person> persons=<span style="color: #000000;"><span style="color: #000000;">Dao.findall (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span><span style="color: #000000;"><span style="color: #000000;">(person Person:persons) {System.out.println (person.tostring ()); } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Bank transfer in a transactional way</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">testtransaction () {personsqliteopenhelper Helper</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Personsqliteopenhelper (getcontext ()); Sqlitedatabase DB</span></span>=<span style="color: #000000;"><span style="color: #000000;">Helper.getwritabledatabase (); Db.begintransaction (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">These two execution codes are either successful</span> or Unsuccessful.</span>Db.execsql ("update person set account=account-1000 where name=?",<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>object[]{"zhangsan"<span style="color: #000000;"><span style="color: #000000;">}); Db.execsql (</span></span>"update person set account=account+1000 where name=?",<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>object[]{"wangwu"<span style="color: #000000;"><span style="color: #000000;">}); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Mark database transaction execution succeeds, default execution is failed, data is not commit,</span> rollback</span><span style="color: #000000;"><span style="color: #000000;">db.settransactionsuccessful (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">finally</span></span><span style="color: #000000;"><span style="color: #000000;">{db.endtransaction (); Db.close (); } }}</span></span></pre><p><p></p></p><p><p>Android JUnit Test Framework</p></p></span>

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.