Android Learning Lesson 18th, Unit Testing

Source: Internet
Author: User

1. Prepare a class to be tested

 Package Com.example.Service;
Public class Personservice { publicvoid Save (String userName) { = Username.substring (5); } Public int Add (int A,int b) { return a +B; }}

2. Create a new unit test project with the option "Android TestProject", select the project you want to test

Create a new class of test cases, note: to inherit from (superclass): Android.test.AndroidTestCase

 Packagecom.example.jnuittest.test;ImportJunit.framework.Assert;ImportCom.example.Service.PersonService;Importandroid.test.AndroidTestCase;/ *-----------------------Personservice is the class to be tested, the following code is a test case--------------------* Write test cases, if the test results are correct, show green, * If the result is wrong, then show Red * in the outline video, right-click the method you want to test*/ Public classJnunittestextendsAndroidtestcase { Public voidTestsave ()throwsexception{Personservice P=NewPersonservice (); P.save ("1234567889"); }         Public voidTestadd ()throwsexception{Personservice P=NewPersonservice (); intactual = P.add (1, 2); Assert.assertequals (3, Actual); }}

Android Learning Lesson 18th, Unit Testing

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.