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
Fork
Restito
Android ABTesting
AndroidTestingPlease

Additional

Language
Kotlin
Version
v1.0.3 (Jul 6, 2018)
Created
Jun 26, 2018
Updated
Sep 14, 2018
Owner
Wu Yu Hao (Ivan) (ivanisidrowu)
Contributor
Wu Yu Hao (Ivan) (ivanisidrowu)
1
Activity
Badge
Generate
Download
Source code

Announcement

Vago

Get bored with writing tedious and dull model tests?

Vago helps you write tests based on POJOs and supports test generation for POJOs transformations.

  • Avoid writing model testing boilerplates
  • Writing tests of POJOs 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)
            }
        })

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.

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 this dependency to app's build.gradle file.

dependencies {
  compile 'com.github.ivanisidrowu:vago:v1.0.3'
}

Contribution

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