The previous article analyzed the test classifications for the Android project, which is a local unit test.
Refer to the official Android documentation.
Before testing, you need to configure the test environment, after the new project, the directory will appear under the app/src/test/java/folder, this folder is used to store the local unit test code (that is, ANDROIDAPI-independent test).
Add test dependencies in the Build.gradle configuration file:
dependencies { //Required--JUnit 4 framework testcompile ' junit:junit:4.12 ' //Optional--Mockito Framewo RK testcompile ' org.mockito:mockito-core:1.10.19 '}
Using the JUNIT4 test requires understanding the next few basic annotations:
@Before:
Use this annotation to do some preparatory work between tests. This code will be called before each test method. Multiple @before blocks can be defined, but the order of multiple calls is indeterminate.
@After:
This annotation defines some cleanup work that is performed at the end of the test. This code is called at the end of each test method. Multiple @after blocks can be defined, but the order of multiple calls is indeterminate.
@Test:
This annotation marks the method of testing, in which a test class can define multiple test methods, each of which requires the use of the @test tag.
@Rule:
Allows you to flexibly add and redefine the behavior of each test method in a reusable way. The rules provided by the Android testing support library, such as Activitytestrule or servicetesterule, are used in Android testing.
@BeforeClass:
Use this annotation to set a static method for the test class, which is executed only once in a test class. This method usually notes some long-consuming operations, such as connecting to a database.
@AfterClass:
Use this annotation to set up a method to execute after the test class has finished running. This method is usually used to release resources allocated within the @beforeclass.
@Test (timeout=)
This annotation is used to set a timeout parameter, which throws an exception if the test has not finished executing within the set time.
In addition, sometimes you need to emulate the Android code, you can use @mock annotations to simulate.
Here is an example of a simple answer on the official website:
Import org.junit.test;import java.util.regex.pattern;import static Org.junit.assert.assertfalse;import static Org.junit.assert.asserttrue;public class Emailvalidatortest { @Test public void Emailvalidator_ Correctemailsimple_returnstrue () { assertthat (emailvalidator.isvalidemail ("[email protected]"), is (true)); } ...}
Use JUnit. Assert method to compare run results and expected values. To make testing more intuitive, you can use Hamcrest (is () and equal ()) to match results and expectations.
Next, take Googlesamples's Android Architecture blueprints as an example to analyze the use of unit tests in the project.
I took the Todo-mvc-dagger Branch code, and the project has two variants (mock and prod). The project structure of the mock is as follows:
The project structure of PROD is as follows
The project is the MVP architecture, separate the business logic from the activity, put it inside the presenter, and the presenter is well suited for local unit testing, which is relatively fast to perform.
Look at the Taskdetailpresentertest test under the mock:
Package Com.example.android.architecture.blueprints.todoapp.taskdetail;import Com.example.android.architecture.blueprints.todoapp.data.task;import Com.example.android.architecture.blueprints.todoapp.data.source.tasksdatasource;import Com.example.android.architecture.blueprints.todoapp.data.source.tasksrepository;import Org.junit.Before;import Org.junit.test;import Org.mockito.argumentcaptor;import Org.mockito.captor;import Org.mockito.InOrder;import Org.mockito.mock;import org.mockito.mockitoannotations;import static Org.mockito.matchers.eq;import static Org.mockito.mockito.inorder;import static org.mockito.mockito.never;import static org.mockito.Mockito.verify; Import Static org.mockito.mockito.when;/** * Unit tests for the implementation of {@link Taskdetailpresenter} */public CLA SS Taskdetailpresentertest {public static final String title_test = "TITLE"; public static final String description_test = "DESCRIPTION"; public static final String invalid_task_id = ""; Public sTatic Final Task Active_task = new Task (title_test, description_test); public static final Task Completed_task = new Task (Title_test, description_test, true); @Mock private Tasksrepository mtasksrepository; @Mock private Taskdetailcontract.view Mtaskdetailview; /** * {@link argumentcaptor} is a powerful Mockito APIs to capture argument values and use them to * perform furthe R actions or assertions on them. */@Captor private argumentcaptor<tasksdatasource.gettaskcallback> mgettaskcallbackcaptor; Private Taskdetailpresenter Mtaskdetailpresenter; @Before public void Setup () {//Mockito have a very convenient inject mocks by using the @Mock annotation . To//inject the mocks in the test, the Initmocks method needs to be called. Mockitoannotations.initmocks (this); The presenter won ' t update the view unless it ' s active. When (Mtaskdetailview.isactive ()). Thenreturn (True); } @Test public void Getactivetaskfromrepositoryandloadintoview () {//When Tasks presenter are asked to open a task Mtaskdetailp Resenter = new Taskdetailpresenter (Active_task.getid (), mtasksrepository, Mtaskdetailview); Mtaskdetailpresenter.start (); Then task was loaded from model, callback are captured and progress indicator is shown verify (mtasksrepository). GE Ttask (eq (Active_task.getid ()), mgettaskcallbackcaptor.capture ()); Inorder inorder = inorder (Mtaskdetailview); Inorder.verify (Mtaskdetailview). Setloadingindicator (True); When the task is finally loaded Mgettaskcallbackcaptor.getvalue (). ontaskloaded (Active_task); Trigger callback/Then progress indicator was hidden and title, description and completion status is shown In UI inorder.verify (Mtaskdetailview). Setloadingindicator (false); Verify (Mtaskdetailview). Showtitle (Title_test); Verify (Mtaskdetailview). Showdescription (Description_TEST); Verify (Mtaskdetailview). Showcompletionstatus (false); }}
This is a complete test of the Task Detail page (I only pasted a @test method). Called Inside the @before method
Mockitoannotations.initmocks (this);
To simulate the taskrepository and Taskdetailcontract.view of the @mock tag.
In the @test test method, instantiate the presenter, and call the Start method, Use the Mockito Verify method to verify that the methods in Taskrepository and Taskdetailcontract.view are called as expected.
Googlesamples inside has the thorough complete test, the road is long, its repair far XI, the embarrassing embarrassment is embarrassing.
Android Test: zero-based 2--local unit test