Skip to content

Instantly share code, notes, and snippets.

View yayaa's full-sized avatar

Yahya Bayramoğlu yayaa

View GitHub Profile
@yayaa
yayaa / MockBuilderWithPairList.kt
Last active October 29, 2017 17:26
How to Test Builder - 5
inline fun <reified T: Any> mockBuilder(itemList: List<Pair<Class<*>, *>>)
= mock<T>(defaultAnswer = AnswerWithPairList(T::class.java, itemList))
// OR
inline fun <reified T: Any> mockBuilder(itemList: List<*>)
= mock<T>(defaultAnswer = AnswerWithPairList(T::class.java,
itemList.map { Pair(it!!::class.java, it) }))
@yayaa
yayaa / AnswerWithPairList.kt
Created October 28, 2017 18:34
How to Test Builder - 4
class AnswerWithPairList(private val clazz: Class<*>, private val pairs: List<Pair<Class<*>, *>>) : Answer<Any> {
private val delegate = ReturnsEmptyValues()
@Throws(Throwable::class)
override fun answer(invocation: InvocationOnMock): Any = invocation.apply {
val returnType = method.returnType
return if (returnType == clazz) mock
else pairs.find { returnType == it.first }?.second ?: delegate.answer(this)
}
}
@yayaa
yayaa / SampleBuilderTest.kt
Last active October 28, 2017 21:36
How to Test Builder - 3
class SampleBuilderTest {
val mockSampleObject = mock<SampleObject>()
val mockSampleBuilder = mockBuilder<SampleObject.Builder>(mockSampleObject)
@Test fun `when super business logic runs should do important stuff with correct configuration`() {
SampleLogic(mockSampleBuilder).superBusinessLogic()
verify(mockSampleBuilder).configA(true)
verify(mockSampleBuilder).configB("something")
@yayaa
yayaa / MockBuilder.kt
Last active October 28, 2017 21:26
How to Test Builder - 2
inline fun <reified T: Any, reified R: Any> mockBuilder(mockResult: R)
= mock<T>(defaultAnswer = AnswerWithPair(T::class.java, R::class.java to mockResult))
@yayaa
yayaa / AnswerWithPair.kt
Last active October 28, 2017 18:06
How to Test Builder - 1
class AnswerWithPair<T : Any>(private val clazz: Class<*>, private val pair: Pair<Class<T>, T>) : Answer<Any> {
private val delegate = ReturnsEmptyValues()
@Throws(Throwable::class)
override fun answer(invocation: InvocationOnMock): Any = invocation.apply {
val returnType = method.returnType
return when (returnType) {
clazz -> mock
pair.first -> pair.second
else -> delegate.answer(this)
public class SimpleApplication extends Application {
@Inject Set<ApplicationPlugin> plugins;
@Override
public void onCreate() {
super.onCreate();
ApplicationComponent.create(this).inject(this);
for (ApplicationPlugin plugin: plugins) {
@Module
@ApplicationScope
public class ApplicationPluginModule {
@Provides
@IntoSet static ApplicationPlugin provideCalligraphyPlugin() {
return new CalligraphyPlugin();
}
@Provides
public class SimpleApplication extends Application {
@Inject CoolAnalyticsTool coolAnalyticsTool;
@Inject ImageLoaderConfiguration imageLoaderConfiguration;
@Inject ImageLoader imageLoader;
@Override
public void onCreate() {
super.onCreate();
ApplicationComponent.create(this).inject(this);
public class SimpleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(true)
.showImageOnFail(android.R.drawable.ic_menu_report_image)
.showImageForEmptyUri(android.R.drawable.ic_menu_report_image)
@yayaa
yayaa / ModifiedToString Template
Created December 28, 2016 09:47
Aligned toString method
public java.lang.String toString() {
#if ( $members.size() > 0 )
#set ( $i = 0 )
return "$classname{"
#foreach( $member in $members )
#if ( $i == 0 )
"##
#else
", ##
#end