Android uses a layered architecture for enterprise applications (1)

Source: Internet
Author: User

Background: when developing enterprise applications using android, it is found that the code structure developed in the traditional mode is relatively poor. The business logic processing and Activity are processed together, and the structure is unclear, the coupling between classes is high, and the functions of classes are complex, which makes unit testing difficult. Therefore, there is a way to ensure the stability of this version, therefore, I have no confidence in my team. Because I don't know when the program will report a bug for no reason, I will summarize the reason for the lack of necessary tracking for the process, leading to fuzzy business behavior. I need to solve these problems with agile management, so I began to restructure.

Reconstruction goals:
The layered architecture is used to decouple classes so that classes follow the single responsibility principle.
Add unit tests to ensure process tracking.
Add automated build tools and integrate code check tools.
Add continuous integration input unit test results and code coverage.


Specific implementation:
Separate business logic processing and Context to form a class independent of android, for example:
SessionManagerService. java
Java code
Public interface SessionManagerService {
// User Logon
Public String login (User user) throws RequestError;
}

As a single interface, it provides a logon operation method.
The implementation class code of this interface will not be pasted. After implementing this interface, you can use unit test to verify this method.
SessionManagerServiceTest. java
Java code
Public class SessionManagerServiceTest extends AndroidTestCase {
 
Private SessionManagerService sms;
 
@ Override
Protected void setUp () throws Exception {
Super. setUp ();
Sms = new SessionManagerServiceImpl ();
}
 
/**
* Test logon successful
*
* @ Throws Exception
*/
@ LargeTest
Public void testLoginSuccess () throws Exception {
User user = new User ();
User. setUserName ("qnlpkuge ");
User. setPassword ("111111 ");
String result = sms. login (user );
AssertEquals ("true", result );
 
}
 
/**
* The test user name does not exist.
*
* @ Throws Exception
*/
@ LargeTest
Public void testUserNotExist () throws Exception {
User user = new User ();
User. setUserName ("qnlpkugedfswe ");
User. setPassword ("1111sdf ");
String result = sms. login (user );
AssertTrue (! "True". equals (result ));
AssertTrue (result. contains ("incorrect user name or password "));
}
 
/**
* The Test password is incorrect.
*
* @ Throws Exception
*/
@ LargeTest
Public void testPasswordError () throws Exception {
User user = new User ();
User. setUserName ("qnlpkuge ");
User. setPassword ("111111 sdfa ");
String result = sms. login (user );
AssertTrue (! "True". equals (result ));
AssertTrue (result. contains ("Incorrect password "));
}
/**
* Exception Test
*/
Public void testValidate (){
Try {
User user = new User ();
User. setUserName ("qnlpkuge ");
Sms. login (user );
Fail ("shocould raise an RequestError ");
} Catch (RequestError e ){
AssertEquals ("the user name or password cannot be blank! ", E. getError ());
AssertTrue (true );
}
}
}

Because android unit testing only supports JUnit3, it is not as convenient as it is not used, and exception testing is also troublesome.
After the unit test is passed, you can directly call this interface in the Activity so that the Activity's responsibilities only need to process the display of data. Of course, there is also a unit test for the Activity. The test on the business logic layer is a function integration test, and the test on the Activity is a interface test, so their test methods are very different. I used a third-party framework, robotium,

LoginActivityTest. java
Java code
Public class LoginActivityTest extends
ActivityInstrumentationTestCase2 <LoginActivity> {
 
Private Solo solo;
 
Public LoginActivityTest (){
Super ("com. goosun. view", LoginActivity. class );
}
 
Public void setUp () throws Exception {
Solo = new Solo (getInstrumentation (), getActivity ());
}
 
Public void testLogin () throws Exception {
Solo. clearEditText (0 );
Solo. enterText (0, "admin ");
Solo. clearEditText (1 );
Solo. enterText (1, "admin ");
GetActivity (). runOnUiThread (new Runnable (){
@ Override
Public void run (){
ImageButton btn = (ImageButton) getActivity (). findViewById (
Com. tianque. seed. R. id. login_option );
Btn. requestFocus ();
}
});
Solo. clickOnImageButton (0 );
Activity f = getInstrumentation (). waitForMonitorWithTimeout (
GetInstrumentation (). addMonitor (MainGrid. class. getName (), null,
False), 9 );
AssertNotNull (f );
AssertEquals (getActivity (). getString (R. string. main_grid), solo
. GetCurrentActivity (). getTitle (). toString ());
Assert. assertTrue (solo. searchText ("Girl management system "));
}
 
@ Override
Public void tearDown () throws Exception {
GetActivity (). finish ();
}
 
}

Author "qnlpkuge"
 

Related Article

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.