I'd like to share three quick and easy Android Studio code templates that make writing unit and UI tests much easier. Because so much of the boilerplate for new test methods and test classes is the same, these templates can save a lot of time.

The first template is an adjustment of the existing code template for JUnit4 Test methods. By setting the method name and "arrange," "act," and "assert" comments, I can easily write a new unit test.

unit_test_template.png

 

@org.junit.Test
public void should${NAME}() throws Exception {
  ${BODY}// TODO
  // arrange
  
  // act
  
  // assert
}


Related: The 10-Step Guide to Annotation Processing in Android Studio

To use this template from inside any test class, type CTRL + Enter to present a method picker, and select Test Method. Android Studio will autocomplete the method signature and place the cursor after "should," allowing me to completely type the method name. Then hitting Tab places the cursor right into the method to continue typing the method body. I'm not sure where I learned this trick, but I've been using it for a while with great success. 



I could reuse this code template for UI tests as well, but I like to write my UI tests a bit different. For starters, they don't have clear "arrange," "act," and "assert" steps, so those comment reminders are not helpful. I also like to word my UI tests differently. I prefer to word them like user interactions so they all start off with asAUser_.

There is not another built-in code template in Android Studio to modify for UI tests. Instead, I have to create my own live template, which brings me to my second code template. I won't go into details about how I created this. The important parts are the full package name of the import and also checking the "use static import if possible" box. By doing these steps, I'll be sure that the import org.junit statement is added to the file header anytime I use this template.

asauser-1.png

 
@org.junit.Test
public void asAUser_$RESTOFMETHODNAME$() throws Exception {
    $END$
}


To use this template with the cursor inside an existing UI test class, start by typing asauser and hit Tab. Android Studio will add the template code and drop the cursor right inline to finish the method name.


Live templates and built-in code templates are pretty cool. Just a few lines of code can save you a few seconds here and there which really adds up. As the saying goes, "A stitch in time saves nine." So, how far can I take this? What other small bits of code do I always find myself retyping or samples that I can never remember the exact structure of?

Related: Level-up with Android Studio Shortcuts and Live Templates

Android Studio won't autocomplete a method name unless its class has already been imported. If you do any Espresso testing, you know that everything is a static method, which means lots of static imports, which means a lot of guessing full package names. This means that for every new Espresso test class I create I have to remember the full package of onView, withText, click and similar methods. It would be nice if these imports were already added to an Espresso test class and then I could just start typing what I wanted. Well a File Template can solve this for me.

espresso_test_class-1.png

import android.support.test.runner.AndroidJUnit4;

import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
public final class ${NAME} {

}


To use this template place the cursor in the project tab (that part on the left) within the androidTest folder and type CTRL + Enter. This will bring up a file picker. Start to type the name of the File Template, in this case EspressoTestClass. Next, hit Enter. Android Studio will create a full test class. Obviously all of these added imports that are not used yet. But using this in conjunction with my previous asauser Live Template, I can fluently type out a test method and use auto complete on all of the Espresso methods.

 

I hope these methods save you a few minutes here and there. The 10 minutes it took to set these up has already been worth it. Be sure to get creative and think about what other code you can auto generate with templates.

Learn more about Android development on backgrounding instead of finishing the root activity in Android:

CLICK HERE

stable-kernel-mobile-app-development

About The Author

Jason Atwood is an Android software engineer at stable|kernel that has been building apps for clients since Jelly Bean. He came to software from the robotics industry where he worked on self-driving cars and flame throwing robots. When he's not working on projects he's reading, writing, and day dreaming about Android development.