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
Vago.testClass(AudioBean::class)Vago.testClass(AudioBean::class, object: Vago.VagoCustomization() {
override fun getDefaultValueByClass(clazz: Class<*>): Any? {
if (clazz.isString()) {
return "1"
}
return super.getDefaultValueByClass(clazz)
}
})- Add
@VagoParcelto 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 {
//...
}- Rebuild your code
- Use generated class to test your parcelable class. In this example, it is
VagoParcelable.testAudioBeanParcelable().
@Test
public void testBean() {
VagoParcelable.testAudioBeanParcelable(customization);
}// without customization
val bean = Vago.createInstance(AudioBean::class)
// with customization
val bean = Vago.createInstance(AudioBean::class, yourCustomization)Vago has annotations to help you test object transformation.
It marks the method which can be tested for object transformation.
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.
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.
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)
}
})
}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? = nullVagoAudioRespVoKt.testToAudio(object: Vago.VagoCustomization() {
override fun getDefaultValueByClass(clazz: Class<*>): Any? {
if (clazz.isString()) {
return "1"
}
return super.getDefaultValueByClass(clazz)
}
})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'
}Contributions are always welcome. If you have any ideas or suggestions, you can contact me or create a github issue.