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