Vago

General

Category
Free
Tag
Testing
License
N/A
Registered
Jun 30, 2018
Favorites
0
Link
https://github.com/ivanisidrowu/vago
See also
OkHttp Idling Resource
Robolectric
android-ab-test-builder
uiautomator
abtestgen

Additional

Language
Kotlin
Version
v1.0.4 (Sep 20, 2018)
Created
Jun 26, 2018
Updated
Apr 23, 2019 (Retired)
Owner
IvanYH Wu (ivanisidrowu)
Contributor
IvanYH Wu (ivanisidrowu)
1
Activity
Badge
Generate
Download
Source code

Advertisement

Vago

Get bored with writing tedious and dull model tests?

Vago helps you write tests based on POJOs, POJOs transformations, and parcelable tests.

  • Avoid writing model testing boilerplates
  • Writing tests of POJOs and Parcelables faster
  • Test object transformation with simple implementation and annotations

Click for examples!

Test General POJOs or Beans

Auto test without customization

Vago.testClass(AudioBean::class)

Auto test with customization

Vago.testClass(AudioBean::class, object: Vago.VagoCustomization() {
            override fun getDefaultValueByClass(clazz: Class<*>): Any? {
                if (clazz.isString()) {
                    return "1"
                }
                return super.getDefaultValueByClass(clazz)
            }
        })

Auto test for parcelables

  1. Add @VagoParcel to the parcelabe class
@VagoParcel
data class AudioBean(var id: Long, var type: Short, var title: String,
                     var mediaUrl: String, var coverUrl: String, var chId: Long) : Parcelable {
            //...
}
  1. Rebuild your code
  2. Use generated class to test your parcelable class. In this example, it is VagoParcelable.testAudioBeanParcelable().
@Test
public void testBean() {
            VagoParcelable.testAudioBeanParcelable(customization);
}

Get Instance directly

// without customization
val bean = Vago.createInstance(AudioBean::class)
// with customization
val bean = Vago.createInstance(AudioBean::class, yourCustomization)

Vago Annotations

Vago has annotations to help you test object transformation.

@VagoMethod

It marks the method which can be tested for object transformation.

@VagoMapping

It establishes attribute name and type mapping for testing transformation. It's useful to map those attributes which have different names and types. Then Vago will generate testing code for you. You can use generated code to test your methods. The exmple below shows you how to use Vago. In the example, AudioRespVo has attribute childId. It also has toAudio() function to do the object transformation. However, to test the transformaiton can be really boring and troublesome. As you can see the childId attribute is transformed to chId in AudioBean. Also, the type tranforms from String to Long. @VagoMapping can help them mapping names and type conversion.

@VagoParcel

It is used to mark the class which is your target class for generating parcelabe test method. If the target class is inner class, you might need to use parentName attribute in @VagoParcel to let Vago knows that this is an inner class.

Example

For instance, you have a value object like this one.

class AudioRespVo {
    var id: Long? = null
        get() = field ?: 0L
    var type: Short? = null
        get() = field ?: 0
    var title: String? = null
        get() = field ?: ""
    var mediaUrl: String? = null
        get() = field ?: ""
    var coverUrl: String? = null
        get() = field ?: ""

    @VagoMapping(name = "chId", type = Long::class)
    var childId: String? = null
        get() = field ?: ""
}

@VagoMethod
fun AudioRespVo.toAudio(): AudioBean {
    return AudioBean(id!!, type!!, title!!, mediaUrl!!, coverUrl!!, childId!!.toLong())
}
data class AudioBean(var id: Long, var type: Short, var title: String,
                     var mediaUrl: String, var coverUrl: String, var chId: Long)

After add annotation to the methods, we need to "REBUILD" project to generate code for testing tranformation. Then use the generated class to test it in unit tests. In this case, the generated class named VagoAudioRespVoKt.

@Test
fun testVoToBean() {
    // VagoAudioRespVoKt is the generated class to help testing.
    VagoAudioRespVoKt.testToAudio(object: Vago.VagoCustomization() {
        override fun getDefaultValueByClass(clazz: Class<*>): Any? {
            if (clazz.isString()) {
                return "1"
            }
            return super.getDefaultValueByClass(clazz)
        }
    })
}

Customization

This is a class that defines your own customization for testing classes. Customization can be put as parameter into Vago.testClass() or any Vago generated classes.

/*
 * Get Default Value by Class.
 * For example, if the class is String, return a default string "test string".
 */
override fun getDefaultValueByClass(clazz: Class<*>): Any? = null

/*
 * Get Default Value of Specific Attribute
 * For example, if the class has an attribute called "id", return a Long with 0L value.
 */
override fun getDefaultValueForSpecificAttribute(attr: String): Any? = null

/*
 * Skip Attributes that you do not want to test
 */
override fun isSkipAttribute(attr: String) = false

/*
 * Get Custom Convert type between different classes
 */
override fun getConvertedType(originalType: Class<*>, wantedType: Class<*>): Any? = null

Example

VagoAudioRespVoKt.testToAudio(object: Vago.VagoCustomization() {
            override fun getDefaultValueByClass(clazz: Class<*>): Any? {
                if (clazz.isString()) {
                    return "1"
                }
                return super.getDefaultValueByClass(clazz)
            }
        })

Download

Add this repo to the root build.gradle file.

allprojects {
  repositories {
  ...
  maven { url 'https://jitpack.io' }
  }
}

Then add these dependencies to app's build.gradle file.

dependencies {
  implementation 'com.github.ivanisidrowu.vago:vago-lib:v1.0.4'
  compileOnly 'com.github.ivanisidrowu.vago:annotation:v1.0.4'
  kapt 'com.github.ivanisidrowu.vago:processor:v1.0.4'
}

Contribution

Contributions are always welcome. If you have any ideas or suggestions, you can contact me or create a github issue.