Android Unit Test

Source: Internet
Author: User

Preface

When the business logic of the program is becoming more and more complex, the problem of the business logic in the test is the most difficult to change. If you have a habit of writing unit tests, unit tests through business logic can easily find the wrong block of code and then resolve it. Today to introduce you to the basic use of unit testing in Android

What the Android unit test can do

The Android Framework provides API for unit testing, first let's take a look at the API architecture diagram

This allows you to see unit tests for Unit tests (Activity,service,contentprovider and application) that the system already supports for important components in Android

The actual work may be a bit more for business logic testing and interface testing
For example, mock some business processes and then write Todo pseudocode, test whether it fits the expectations, and then do the UI implementation. First of all, ensure that our business logic does not go wrong, so as to ensure that our program will not be a big bug

How to use

Test unit tests for business logic
When the test does not need to reference the internal resources of the app, we can use org.junit.Test, this way does not install the app to the phone
1. We create a new operationserviceunittest

ImportOrg.junit.After;ImportOrg.junit.Before;ImportOrg.junit.Test;Import Staticorg.junit.assert.*;/** * Created by Qinwei on 16/5/6 pm 3:15 * * Public  class operationserviceunittest {    PrivateOperationservice Service;@Before     Public void setUp()throwsException {//TODO do some test configurationService=NewOperationservice (); }@Test     Public void Testadd()throwsException {assertequals (1, Service.add (1,1)) ; }@Test     Public void testmultiplicative()throwsException {assertequals (2, Service.multiplicativ (1,1)) ; }@After     Public void TearDown()throwsException {//TODO Test End Call}

2. Write a method Testadd method with @test annotation decoration

    @Test    publicvoidtestAddthrows Exception {        service=new OperationService();        //对测试结果做一个预期判断        assertEquals(2,service.add(1,1)) ;    }

We can see the interface after the test passes.

Test does not pass

When it comes to unit testing of the app's internal resources, this way installs the app to the phone
1. Create a androidtestcase subclass

/** * Created by qinwei on 16/5/6 下午3:48 */publicclass LoginTest extends AndroidTestCase {    publicvoidtestLoginthrows Exception {        String username = getContext().getString(R.string.username);        String password = getContext().getString(R.string.password);        new LoginBiz();        assertEquals(true, biz.doLogin(username, password));    }}

Using the GetContext method to get the context object, you can use this object for database operation, SP operation, get string.xml file data, etc.

-------------activity unit test to be continued------------

Conclusion

There are some places in the text that are wrong and ask you to treatise.

Android Unit Test

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.