In my previous article, I explained how to get started with unit testing. If you haven’t read it yet, you can follow Build local unit tests for your android apps. It will provide you with the fundamentals to get started.
Create a login activity class
After you have created your class, ensure your code looks like the one below:
- LoginActivity.java
public class LoginActivity extends AppCompatActivity { Button btnLogin; EditText userName; EditText password; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); btnLogin=(Button)findViewById(R.id.btnLogin); userName=(EditText)findViewById(R.id.edtUsername); password=(EditText)findViewById(R.id.edtPassword); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { validate(userName.getText().toString(),password.getText().toString()); } }); } public LoginActivity(Context context){ } public String validate(String userName, String password) { if(userName.equals("user") && password.equals("user")) return "Login was successful"; else return "Invalid login!"; }
- Create a Login xml layout
login.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/edtPassword" android:layout_width="0dp" android:layout_height="60dp" android:layout_marginBottom="50dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="50dp" android:ems="10" android:hint="Password" android:inputType="textPersonName" app:layout_constraintBottom_toTopOf="@+id/btnLogin" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/edtUsername" /> <EditText android:id="@+id/edtUsername" android:layout_width="0dp" android:layout_height="60dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="47dp" android:ems="10" android:hint="Username" android:inputType="textPassword" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnLogin" android:layout_width="0dp" android:layout_height="60dp" android:layout_marginBottom="360dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:text="Login" android:textColor="@android:color/white" android:background="@color/colorPrimaryDark" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/edtUsername" /> </android.support.constraint.ConstraintLayout>
Now that we are done with our login module. It’s time to run a unit test to ensure that our logic returns our desired result.
You got that right?
Great!!!
Lets’ create our unit test class in app>src>test>java
@RunWith(MockitoJUnitRunner.class) public class LoginUnitTest { private static final String FAKE_STRING = "Login was successful"; @Mock Context mMockContext; @Test public void readStringFromContext_LocalizedString() { LoginActivity myObjectUnderTest = new LoginActivity(mMockContext); // ...when the string is returned from the object under test... String result = myObjectUnderTest.validate("user","user"); // ...then the result should be the expected one. assertThat(result, is(FAKE_STRING)); } }
Remember in the login class validate method returns a string based on user’s input. If the username and password is user, it should return Login was successful else invalid login.
The FAKE_STRING in login unit test class is a constant string used to confirm the result of my method validate(). So assertThat(result, is(FAKE_STRING)) simply means, assert that the result of my validate method is the same as my expected result which is FAKE_STRING.
You got that right?
To run the test right click on the Login unit test class and then select run
You should have this result.
Now let us alter the login credentials in our unit test class
You should have an error this time!
This is because, the validate method returned invalid login which isn’t the same as the expected FAKE_STRING.
You have successfully created your first Unit Test for an android project.
It’s important to note that, an effective and error free unit test is dependent on your code structure. it’s not ideal to complete a project before you create your unit test classes. You eventually mess up the whole system.
Best practice, create your unit test for every single module you build in your project. For instance, you can create a unit test for a login module on your app.
Every single logic must be tested. This saves you a million hours of debugging.
No Comment! Be the first one.