Search code examples
javaandroid-studiotestingjunit

Create a Java Unit Test from a Login class written in Android Studio


can someone please help me with a java unit test for this class. I've tried many times but the result is not working, so I hope that you can help me.

public class Login extends AppCompatActivity {

    EditText email, password;
    Button login_button;
    TextView goToReg;
    ProgressBar progressBar;
    FirebaseAuth fAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_login );

        email = findViewById( R.id.emailLog );
        password = findViewById( R.id.passwordLog );
        login_button = findViewById( R.id.buttonLog );
        goToReg = findViewById( R.id.gotoregistration );
        progressBar = findViewById( R.id.progressBarLog );

        fAuth = FirebaseAuth.getInstance();

        login_button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mail = email.getText().toString().trim();
                String password_string = password.getText().toString().trim(); 

                if(TextUtils.isEmpty(mail))
                {
                    email.setError("Email requested!");
                    return;
                }
                if(TextUtils.isEmpty( password_string ))
                {
                    password.setError( "Password requested!" );
                    return;
                }
                if(password_string.length()<6)
                {
                    password.setError( " password must have more than 5 characters" );
                    return;
                }
                progressBar.setVisibility( View.VISIBLE );

                // User authentication
                fAuth.signInWithEmailAndPassword( mail, password_string ).addOnCompleteListener( new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()) {
                            Toast.makeText( Login.this, "Login done.", Toast.LENGTH_SHORT ).show();
                            startActivity( new Intent( getApplicationContext(), MainActivity.class ) );
                        }else{
                            Toast.makeText( Login.this, "Error: "+task.getException().getMessage(), Toast.LENGTH_SHORT ).show();
                            progressBar.setVisibility( View.INVISIBLE );
                        }
                    }
                });
            }
        } );

        goToReg.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity( new Intent( getApplicationContext(), Registration.class ) );
            }
        } );
    }
}

This is a Login class for my app written on Android Studio, there are also others classes that I have to test and are similiar to this class, so I hope someone can make an example using this Login class and create a JUnit test example, so that I can understand the process. Thank you.


Solution

  • You would simply test if your login works according to the data you send in.

    Please view the following here for further advice. For example, it would be something like:

    email = findViewById( R.id.emailLog );
    password = findViewById( R.id.passwordLog );                
    email.setText("email");
    pass.setText("pass");
    login_button = findViewById( R.id.buttonLog );
    loginBtn.performClick();
    assertTrue(loginActivity.isCurUserLoggedIn());
    

    I haven't tested this so do let me know if this example works for you.