Boost productivity for testing experience with Custom Live Template

Thaw Zin Toe
ProAndroidDev
Published in
3 min readApr 6, 2024

--

Today we’re going to share and learn more about how to boost productivity with our own custom templates to make testing in our project easier.

I won’t go deep dive into Live Template. If you want to know more about Live Template, you can find more details in this article:

First things First

I created a few custom templates to boost my productivity based on my experiences.

I was practicing applying TDD in our code a few months ago. I know TDD is hard and time-consuming for beginners, but if we are more familiar with writing test cases, we can more easily write them down.

When we apply the test cases in our project, there is a lot of code duplication involved in development, such as ,“unit test function”, “mock a function” and “assertion”.

In this approach , we will do with AAA (Arrange-Act-Assert) pattern, has become almost a standard across the industry. (Note. we can apply every pattern based on our company architecture and rules)

Every software practice is just a guideline, not a rigid rule

Custom LiveTemplate: Unit test function name

  • Abbreviation: utest
  • Description: Use this JUnit test template to easily structure our unit tests. Simply fill in placeholders for test details and code snippets for arranging, acting, and asserting, following the Arrange-Act-Assert pattern.
  • Template:
@org.junit.Test
fun $testMethodName$_$stateUnderTest$_$expectedBehavior$() {
// Arrange
$arrangeCode$

// Act
$actCode$

// Assert
$assertCode$
}
  • Applicable Contexts: Android & Kotlin files.
  • Edit Variables:
  • testMethodName — method name
  • stateUnderText — specifies the setup or preparation needed before the test can be executed.
  • expectedBehavior — the expected outcome or behavior of the system or object under test when the test is performed
  • arrangeCode — This placeholder is where we would include code to set up the initial state or conditions required for the test.
  • actCode — This placeholder is where we would include the code that triggers the specific action or behavior being tested.
  • assertCode — This placeholder is where we would include code to verify the outcome or behavior of the system under test.

Custom Live Template: mockk function for stub

  • Abbreviation: mkstub
  • Description: Sets up a stub behavior for a method call on a MockK mock object.
  • Template:
io.mockk.every { $mock$.$methodCall$ } returns $returnValue$
  • Applicable Contexts: Android & Kotlin files.
  • Edit Variables:
  • mock — mock object on which the stub behavior is being defined.
  • methodCall — actual method call that we want to stub on the mock object
  • returnValue — the value that should be returned when the specified method is called on the mock object.

By using variables in the unit test live template, we can easily create structured and organized test methods that follow the Arrange-Act-Assert pattern, making our test code more readable and maintainable. We can change our custom live template based on our situation. The above custom templates are my own idea to boost productivity when writing unit test cases in Android Studio.

I have already uploaded my few custom Live-templates to Google Drive:

You can contribute with your own custom live template to boost our productivity. I would really appreciate it if you want to.

See you next time. Bye Bye 👋

--

--