我想在Android Studio中为此类编写单元测试,但由于使用了Firebase,我在创建测试以覆盖userignin()和getSignedinUserprofile()时遇到问题.有人对这个有经验么?
public class LoginActivity extends AppCompatActivity implements VIEw.OnClickListener{private button buttonSignIn;private EditText editTextEmail;private EditText editTextPassword;private TextVIEw textVIEwSignUp;private ProgressDialog mProgressDialog;private FirebaseAuth mfireBaseAuth;private DatabaseReference mDatabase;private String userID;private FirebaseUser firebaseUser;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); FirebaseApp.initializeApp(this); 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);}@OverrIDepublic voID onClick(VIEw v) { if(v == buttonSignIn){ usersignin();} if(v==textVIEwSignUp){ startActivity(new Intent(this, Registeractivity.class));}}public 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(); } } });}public 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 } });}public voID checkStudentOrProfessor(User user) { Intent i; if (user.isstudent ) { i = new Intent(this, MainActivityStudent.class); } else { i = new Intent(this, MainActivityProfessor.class); } startActivity(i);}
解决方法:
您可以使用Mockito和powermockito来模拟Firebase依赖项.对于模拟静态函数(如FirebaseDatabase.getInstance()),必须使用powermockrunner来运行测试,但稍后可以委托给不同的运行程序.
package com.test.firebasetest;import com.Google.firebase.database.DataSnapshot;import com.Google.firebase.database.DatabaseReference;import com.Google.firebase.database.FirebaseDatabase;import com.Google.firebase.database.ValueEventListener;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.Mockito;import org.mockito.invocation.InvocationOnMock;import org.mockito.stubbing.Answer;import org.powermock.API.mockito.powermockito;import org.powermock.core.classloader.annotations.PrepareForTest;import org.powermock.modules.junit4.powermockrunner;import static org.mockito.Matchers.any;import static org.mockito.Matchers.anyString;import static org.mockito.Mockito.doAnswer;import static org.mockito.Mockito.when;@RunWith(powermockrunner.class)@powermockrunnerDelegate(JUnit4.class)@PrepareForTest({ FirebaseDatabase.class})public class LoginActivityTest { private DatabaseReference mockedDatabaseReference; @Before public voID before() { mockedDatabaseReference = Mockito.mock(DatabaseReference.class); FirebaseDatabase mockedFirebaseDatabase = Mockito.mock(FirebaseDatabase.class); when(mockedFirebaseDatabase.getReference()).thenReturn(mockedDatabaseReference); powermockito.mockStatic(FirebaseDatabase.class); when(FirebaseDatabase.getInstance()).thenReturn(mockedFirebaseDatabase); } @Test public voID getSignedInUserProfiletest() { when(mockedDatabaseReference.child(anyString())).thenReturn(mockedDatabaseReference); doAnswer(new Answer<VoID>() { @OverrIDe public VoID answer(InvocationOnMock invocation) throws Throwable { ValueEventListener valueEventListener = (ValueEventListener) invocation.getArguments()[0]; DataSnapshot mockedDataSnapshot = Mockito.mock(DataSnapshot.class); //when(mockedDataSnapshot.getValue(User.class)).thenReturn(testOrMockedUser) valueEventListener.onDataChange(mockedDataSnapshot); //valueEventListener.onCancelled(...); return null; } }).when(mockedDatabaseReference).addListenerForSingleValueEvent(any(ValueEventListener.class)); new LoginActivity().getSignedInUserProfile(); // check preferences are updated }}
您需要为模块build.gradle添加适当的依赖项:
testCompile 'junit:junit:4.12'testCompile 'org.powermock:powermock:1.6.5'testCompile 'org.powermock:powermock-module-junit4:1.6.5'testCompile 'org.powermock:powermock-API-mockito:1.6.5'
总结 以上是内存溢出为你收集整理的java – 使用Firebase的Android Studio中的JUnit测试全部内容,希望文章能够帮你解决java – 使用Firebase的Android Studio中的JUnit测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)