This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T: Any, reified R: Any> mockBuilder(mockResult: R) | |
= mock<T>(defaultAnswer = AnswerWithPair(T::class.java, R::class.java to mockResult)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SimpleApplication extends Application { | |
@Inject Set<ApplicationPlugin> plugins; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
ApplicationComponent.create(this).inject(this); | |
for (ApplicationPlugin plugin: plugins) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
@ApplicationScope | |
public class ApplicationPluginModule { | |
@Provides | |
@IntoSet static ApplicationPlugin provideCalligraphyPlugin() { | |
return new CalligraphyPlugin(); | |
} | |
@Provides |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public java.lang.String toString() { | |
#if ( $members.size() > 0 ) | |
#set ( $i = 0 ) | |
return "$classname{" | |
#foreach( $member in $members ) | |
#if ( $i == 0 ) | |
"## | |
#else | |
", ## | |
#end |