2017-03-31 81 views
1

是否有任何人使用Android Studio爲java中的On Create和OnClick方法創建單元測試有任何經驗?我使用我的Firebasedatabase的數據。 我想我必須使用模擬對象,但我不會從哪裏開始。針對Android活動的Junit測試

下從我的類LoginActivity

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     mfireBaseAuth = FirebaseAuth.getInstance(); 
     mDatabase = FirebaseDatabase.getInstance().getReference(); 
     mProgressDialog = new ProgressDialog(this); 
     editTextEmail = (EditText) findViewById(R.id.editTextEmail); 
     editTextPassword = (EditText) findViewById(R.id.editTextPassword); 
     buttonSignIn = (Button) findViewById(R.id.buttonSignIn); 
     textViewSignUp = (TextView) findViewById(R.id.textViewSignUp); 

     buttonSignIn.setOnClickListener(this); 
     textViewSignUp.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 
     if(v == buttonSignIn){ 
      usersignin(); 

     } 
     if(v==textViewSignUp){ 
      startActivity(new Intent(this, RegisterActivity.class)); 
     } 
     } 

    private void usersignin() { 
     String email = editTextEmail.getText().toString().trim(); 
     String password = editTextPassword.getText().toString().trim(); 

     if(TextUtils.isEmpty(email)){ 
      Toast.makeText(this, "Please enter Email", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     if(TextUtils.isEmpty(password)){ 
      Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     mProgressDialog.setMessage("Logging in. Please wait..."); 
     mProgressDialog.show(); 
     mfireBaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
      @Override 
      public void onComplete(@NonNull Task<AuthResult> task) { 
       mProgressDialog.dismiss(); 
       if(task.isSuccessful()){ 
        getSignedInUserProfile(); 
       } 
      } 
     }); 
    } 

    private void getSignedInUserProfile() { 

     DatabaseReference reference = mDatabase;//.child("eduback-2feef"); 
     firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); 
     userID = firebaseUser.getUid(); 
     reference.child("Users").child(userID).child("User info").addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       User user = dataSnapshot.getValue(User.class); 
       if(user != null) { 
        // Save if the user is student or prof in shared prefs. 
        PreferenceHelper helper = new PreferenceHelper(getBaseContext()); 
        helper.setIsStudent(user.isStudent); 
        checkStudentOrProfessor(user); 
       } 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       // Ups vis error 
      } 
     }); 



    } 

    private void checkStudentOrProfessor(User user) { 

     Intent i; 
     if (user.isStudent) { 
      i = new Intent(this, MainActivityStudent.class); 
     } else { 
      i = new Intent(this, MainActivityProfessor.class); 
     } 
     startActivity(i); 


    } 



} 

回答

0

代碼Java的嘲諷的Mockito框架提供與Android單元測試兼容性。

您可以配置模擬對象以在調用時返回某個特定值。

你不需要測試Firebasedatabass。

但你必須做出Firebasedatabase包裝類爲你的瀏覽

包裝類必須包括變量(用戶ID,user.isStudent)

+0

你能給我一個例子嗎? – MeryT