从在 Android Studio 中编写的登录名 class 创建 Java 单元测试
Create a Java Unit Test from a Login class written in Android Studio
有人可以帮我做一个 java 这个 class 的单元测试吗?
试了很多次都不行,希望大家能帮帮我。
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 ) );
}
} );
}
}
这是我在 Android Studio 上编写的应用程序的登录 class,还有其他 class 我必须测试并且与此类似的 class ], 所以我希望有人可以使用这个登录 class 做一个例子,并创建一个 JUnit 测试例子,这样我就可以理解这个过程。
谢谢。
您只需根据您发送的数据测试您的登录是否有效。
请查看以下内容here以获取更多建议。例如,它将是这样的:
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());
我还没有测试过这个,如果这个例子适合你,请告诉我。
有人可以帮我做一个 java 这个 class 的单元测试吗? 试了很多次都不行,希望大家能帮帮我。
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 ) );
}
} );
}
}
这是我在 Android Studio 上编写的应用程序的登录 class,还有其他 class 我必须测试并且与此类似的 class ], 所以我希望有人可以使用这个登录 class 做一个例子,并创建一个 JUnit 测试例子,这样我就可以理解这个过程。 谢谢。
您只需根据您发送的数据测试您的登录是否有效。
请查看以下内容here以获取更多建议。例如,它将是这样的:
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());
我还没有测试过这个,如果这个例子适合你,请告诉我。