微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单元测试中的调用目标异常

如何解决单元测试中的调用目标异常

我是单元测试的新手。请让我了解错误发生在哪里。

FirebaeAuthRespository.java

    public mutablelivedata<IBDataWrapper<User>> signInWithEmailPassword(FirebaseAuth firebaseAuth,String email,String password){

    mutablelivedata<IBDataWrapper<User>> authenticatedUsermutablelivedata = new mutablelivedata<>();
    firebaseAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if(task.isSuccessful()){
                FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
                if (firebaseUser != null) {
                  

              authenticatedUsermutablelivedata.setValue(IBDataWrapper.success(firebaseUser,null));
                }
            }else{
                authenticatedUsermutablelivedata.setValue(IBDataWrapper.error(task.getException().getMessage(),null,null));
            }
        }
    });
   return authenticatedUsermutablelivedata;
}

视图模型

public class Loginviewmodel extends Androidviewmodel {

mutablelivedata<IBDataWrapper<User>> authenticatedUsermutablelivedata;
private IBFirebaseAuthRepository mIBFirebaseAuthRepository;

public mutablelivedata<IBDataWrapper<User>> ibAuthenticatedUserLiveData;
FirebaseAuth mFirebaseAuth;

public Loginviewmodel(Application application) {
    super(application);
    mIBFirebaseAuthRepository = new IBFirebaseAuthRepository();
}

public Loginviewmodel(Application application,FirebaseAuth firebaseAuth) {
    super(application);

    mFirebaseAuth = firebaseAuth;
    mIBFirebaseAuthRepository = new IBFirebaseAuthRepository();
}

public void authenticateUser(String email,String password){
   mFirebaseAuth = FirebaseAuth.getInstance();
    ibAuthenticatedUserLiveData = mIBFirebaseAuthRepository.signInWithEmailPassword(mFirebaseAuth,email,password);
}


}

登录活动.java

   private void initLoginviewmodel(){
    mLoginviewmodel =  new viewmodelProvider(this).get(Loginviewmodel.class);
}
 private void signInWithEmailPassword(String email,String password)
{
    // Input fields validation
    if(email.isEmpty() || password.isEmpty()){
        showSnackBar("Please enter your credentials");
        return;
    }

    // Network Connectivity check
    if(!new IBNetworkCheck(this).isNetworkAvailable()){
        showErrorDialog(0,Common.ERROR_TITLE_NETWORK,Common.ERROR_MSG_NETWORK);
        return;
    }

    ProgressBarHandler pbHandler = new ProgressBarHandler(this);
    pbHandler.show();
    mLoginviewmodel.authenticateUser(email,password);
    mLoginviewmodel.ibAuthenticatedUserLiveData.observe(this,userIBDataWrapper -> {

        pbHandler.hide();
        User user =  userIBDataWrapper.data;
        if(user != null){

            // Get USer profile
            mLoginviewmodel.getUserProfile(user.getUserId());
            mLoginviewmodel.ibUserProfileLiveData.observe(this,userProfileDataWrapper ->{

                if(userProfileDataWrapper.boolSatuts == IBDataWrapper.BoolStatus.TRUE){
                    User userWithProfile = userProfileDataWrapper.data;
                    if(userWithProfile != null){

                        Common.currentUser = userWithProfile;
                        goToHomeActivity();
                        finish();
                    }
                    else{
                        IBLogger.logErrorMessage("IntiBhojanam","[Error Occurred 1] - Retrieving User Profile");
                    }

                }
                else{
                    IBLogger.logErrorMessage("IntiBhojanam","[Error Occurred 2] - Retrieving User Profile");
                }
            });
        }
        else{
            IBLogger.logErrorMessage("Test"," Login Failed");
            String msg = userIBDataWrapper.message;
            if(Objects.requireNonNull(msg).contains("network error")){
                showErrorDialog(0,Common.ERROR_MSG_NETWORK);
            }
            else if(msg.contains("no user record")){
                showErrorDialog(0,"Error","There is no user registered for the entered account information. Please recheck and try again!");
            }
            else{
                showErrorDialog(0,msg);
            }
            //showSnackBar(userIBDataWrapper.message);
        }
    });
}

登录viewmodelTest.java

@RunWith(powermockrunner.class)
@powermockrunnerDelegate(JUnit4.class)
@PrepareForTest({ FirebaseDatabase.class})
public class LoginviewmodelTest {

private DatabaseReference mockedDatabaseReference;
private FirebaseAuth mockAuth;

private Loginviewmodel mockLoginviewmodel;

@Before
public void before() {
    mockedDatabaseReference = Mockito.mock(DatabaseReference.class);

    mockAuth = mock(FirebaseAuth.getInstance().getClass());
    mockLoginviewmodel = mock(Loginviewmodel.class);

    FirebaseDatabase mockedFirebaseDatabase = Mockito.mock(FirebaseDatabase.class);
    when(mockedFirebaseDatabase.getReference()).thenReturn(mockedDatabaseReference);

    powermockito.mockStatic(FirebaseDatabase.class);
    when(FirebaseDatabase.getInstance()).thenReturn(mockedFirebaseDatabase);
}

@Test
public void test_firebase_auth(){
    mockLoginviewmodel.authenticateUser("abc@gmail.com","123456");
  //  verify(mockAuth).createuserWithEmailAndPassword("abc@gmail.com","123456");

    }
}

我收到此错误

org.objenesis.ObjenesisException: java.lang.reflect.InvocationTargetException

调用视图模型测试firebas登录注册功能错误是什么。请告诉我。提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。