Skip to content

Instantly share code, notes, and snippets.

View canergulgec's full-sized avatar

Caner Gülgeç canergulgec

View GitHub Profile
//UI Test
androidTestImplementation "androidx.test:core-ktx:$test_core"
androidTestImplementation "androidx.test.ext:junit-ktx:$test_ext"
androidTestImplementation "androidx.test:rules:$test_rules"
androidTestImplementation "androidx.test:runner:$test_runner"
androidTestImplementation "androidx.test.espresso:espresso-core:$espresso"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso"
//Kakao
androidTestImplementation "com.agoda.kakao:kakao:$kakao"
@HiltAndroidApp
open class MovieDbApp : Application() {
open fun getBaseUrl() = BuildConfig.BASE_URL
}
@InstallIn(SingletonComponent::class)
@Module
class RetrofitModule {
@Provides
@Named("BASE_URL")
fun provideBaseUrl(@ApplicationContext appContext: Context): String {
return (appContext as MovieDbApp).getBaseUrl()
}
class MovieTestApp : MovieDbApp() {
companion object{
const val port = 8080
}
override fun getBaseUrl() = "http://localhost:$port"
}
class MovieTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, className: String?,
context: Context?): Application {
return super.newApplication(cl, MovieTestApp::class.java.name, context)
}
}
@LargeTest
class MovieFragmentTest {
private lateinit var activityScenario: ActivityScenario<MainActivity>
private val mockWebServer = MockWebServer()
@Before
fun setup() {
mockWebServer.start(port)
IdlingRegistry.getInstance().register(
OkHttp3IdlingResource.create(
{
"results": [
{
"adult": false,
"backdrop_path": "/srYya1ZlI97Au4jUYAktDe3avyA.jpg",
"genre_ids": [
14,
28,
12
],
object FileReader {
fun readTestResourceFile(fileName: String): String {
try {
val inputStream = (InstrumentationRegistry.getInstrumentation().targetContext
.applicationContext as MovieTestApp).assets.open(fileName)
val builder = StringBuilder()
val reader = InputStreamReader(inputStream, "UTF-8")
reader.readLines().forEach {
builder.append(it)
}
@LargeTest
class MovieFragmentTest {
private val mockWebServer = MockWebServer()
@Before
fun setup() {
mockWebServer.start(port)
IdlingRegistry.getInstance().register(
OkHttp3IdlingResource.create(
"okhttp",
object OkHttpProvider {
// Timeout for the network requests
private const val REQUEST_TIMEOUT = 3L
private var okHttpClient: OkHttpClient? = null
fun getOkHttpClient(): OkHttpClient {
return if (okHttpClient == null) {
val okHttpClient = OkHttpClient.Builder()