Skip to content

Commit

Permalink
15 refactoring your tests (#11)
Browse files Browse the repository at this point in the history
* final project updates

* forgot to add gitkeep for androidTest

* another .gitkeep needed
  • Loading branch information
lgleasain committed Jul 4, 2021
1 parent f690baf commit b7136f0
Show file tree
Hide file tree
Showing 73 changed files with 431 additions and 520 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs.kotlin"

android {
compileSdkVersion 29
compileSdkVersion 30
defaultConfig {
applicationId "com.raywenderlich.codingcompanionfinder"
minSdkVersion 21
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -28,36 +28,33 @@ android {
enabled = true
}

sourceSets {
String sharedTestDir = 'src/sharedTest/java'
String sharedResources = 'src/sharedTest/assets'
test {
java.srcDirs += sharedTestDir
resources.srcDirs += sharedResources
}
androidTest {
java.srcDirs += sharedTestDir
resources.srcDirs += sharedResources
}
}

testOptions {
unitTests.includeAndroidResources = true
unitTests.returnDefaultValues = true
}

android {
sourceSets {
String sharedTestDir = 'src/sharedTest/java'
String sharedResources = 'src/sharedTest/assets'
test {
java.srcDir sharedTestDir
resources.srcDirs += sharedResources
}
androidTest {

java.srcDir sharedTestDir
resources.srcDirs += sharedResources
}
}
}
}

dependencies {
// android stuff
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

Expand All @@ -66,66 +63,62 @@ dependencies {
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0" // use -ktx for

// Glide
implementation("com.github.bumptech.glide:glide:4.9.0") {
implementation("com.github.bumptech.glide:glide:4.12.0") {
exclude group: "com.android.support"
}
kapt 'com.github.bumptech.glide:compiler:4.9.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'

// carouselview library
implementation "com.synnapps:carouselview:0.1.5"

// retrofit
implementation "com.squareup.okhttp3:logging-interceptor:3.12.0"
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
implementation "com.squareup.okhttp3:logging-interceptor:3.14.9"
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

// eventbus
implementation 'org.greenrobot:eventbus:3.1.1'

// Architecture components
def lifecycle_version = "2.1.0"
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

// Koin
implementation 'org.koin:koin-android-viewmodel:1.0.1'
androidTestImplementation("org.koin:koin-test:1.0.1")
{ exclude(group: "org.mockito") }
androidTestImplementation "org.mockito:mockito-android:2.28.2"
implementation 'org.koin:koin-android-viewmodel:2.0.1'
androidTestImplementation 'org.koin:koin-test:2.0.1'

def coroutinesVersion = "1.5.0"

// testing dependencies
testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test:runner:1.2.0'
testImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation "androidx.test:rules:1.2.0"
testImplementation "androidx.test.ext:junit:1.1.1"
testImplementation "android.arch.navigation:navigation-testing:1.0.0-alpha08"
testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.test:runner:1.3.0'
testImplementation 'androidx.test.espresso:espresso-core:3.3.0'
testImplementation "androidx.test:rules:1.3.0"
testImplementation "androidx.test.ext:junit:1.1.2"
testImplementation "androidx.navigation:navigation-testing:2.3.5"
testImplementation 'com.squareup.okhttp3:mockwebserver:3.12.0'
testImplementation "androidx.test.espresso:espresso-contrib:3.2.0"
testImplementation 'org.koin:koin-test:1.0.1'
testImplementation 'org.robolectric:robolectric:4.3'
testImplementation "androidx.arch.core:core-testing:2.0.1"
testImplementation 'androidx.test:core:1.2.0'

androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation "androidx.test:rules:1.2.0"
androidTestImplementation "androidx.test.ext:junit:1.1.1"
androidTestImplementation "android.arch.navigation:navigation-testing:1.0.0-alpha08"
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.12.0'
androidTestImplementation "androidx.test.espresso:espresso-contrib:3.2.0"
androidTestImplementation "org.robolectric:annotations:4.3"
androidTestImplementation "androidx.arch.core:core-testing:2.0.1"
// Once https://issuetracker.google.com/127986458 is fixed this can betestImplementation
// fragmentscenario testing
debugImplementation 'androidx.fragment:fragment-testing:1.1.0-beta01'
debugImplementation "androidx.test:core:1.2.0"

def coroutinesVersion = '1.1.0'
testImplementation "androidx.test.espresso:espresso-contrib:3.3.0"
testImplementation 'org.robolectric:robolectric:4.5.1'
testImplementation 'org.koin:koin-test:2.0.1'
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"

// Once https://issuetracker.google.com/127986458 is fixed this can be testImplementation
// fragmentscenario testing
debugImplementation 'androidx.fragment:fragment-testing:1.3.4'
debugImplementation "androidx.test:core:1.3.0"

androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation "org.robolectric:annotations:4.5.1"
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation "androidx.test:rules:1.3.0"
androidTestImplementation "androidx.test.ext:junit:1.1.2"
androidTestImplementation "androidx.navigation:navigation-testing:2.3.5"
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.12.0'
androidTestImplementation "androidx.test.espresso:espresso-contrib:3.3.0"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,11 +30,21 @@
package com.raywenderlich.codingcompanionfinder

import android.app.Application
import org.koin.android.ext.android.startKoin
import android.util.Log
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import org.koin.core.error.KoinAppAlreadyStartedException

class CodingCompanionFinder: Application(){
class CodingCompanionFinder : Application() {
override fun onCreate() {
super.onCreate()
startKoin(this, listOf(appModule, urlsModule))
try {
startKoin {
androidContext(this@CodingCompanionFinder)
modules(listOf(appModule, urlsModule))
}
} catch (koinAlreadyStartedException: KoinAppAlreadyStartedException) {
Log.i("CodingCompanionFinder", "KoinAppAlreadyStartedException, should only happen in tests")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -29,22 +29,22 @@
*/
package com.raywenderlich.codingcompanionfinder

import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.raywenderlich.codingcompanionfinder.retrofit.AuthorizationInterceptor
import com.raywenderlich.codingcompanionfinder.retrofit.PetFinderService
import com.raywenderlich.codingcompanionfinder.searchforcompanion.SearchForCompanionViewModel
import com.raywenderlich.codingcompanionfinder.searchforcompanion.ViewCompanionViewModel
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.android.viewmodel.ext.koin.viewModel
import org.koin.dsl.module.module
import org.koin.android.viewmodel.dsl.viewModel
import org.koin.core.qualifier.named
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

const val PETFINDER_URL = "PETFINDER_URL"
val urlsModule = module {
single(name = PETFINDER_URL){MainActivity.DEFAULT_PETFINDER_URL}
single(named(PETFINDER_URL)){MainActivity.DEFAULT_PETFINDER_URL}
}
val appModule = module {
single<PetFinderService> {
Expand All @@ -56,9 +56,8 @@ val appModule = module {
.addInterceptor(AuthorizationInterceptor())
.build()
Retrofit.Builder()
.baseUrl(get(PETFINDER_URL) as String)
.baseUrl(get<String>(named(PETFINDER_URL)))
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.client(client)
.build().create(PetFinderService::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,7 +35,6 @@ import android.os.Bundle
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.raywenderlich.codingcompanionfinder.retrofit.AuthorizationInterceptor
import com.raywenderlich.codingcompanionfinder.retrofit.PetFinderService
import com.raywenderlich.codingcompanionfinder.testhooks.IdlingEntity
Expand Down Expand Up @@ -72,7 +71,6 @@ class MainActivity : AppCompatActivity() {
petFinderService = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.client(client)
.build().create(PetFinderService::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -43,7 +43,6 @@ import com.raywenderlich.codingcompanionfinder.MainActivity
import com.raywenderlich.codingcompanionfinder.R
import com.raywenderlich.codingcompanionfinder.models.Animal
import com.synnapps.carouselview.CarouselView
import com.synnapps.carouselview.ViewListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
Expand All @@ -70,8 +69,7 @@ class RandomCompanionFragment : Fragment() {
key = (activity as MainActivity).accessToken
(activity as MainActivity).petFinderService?.let { petFinderService ->

val animalsRequest = petFinderService.getAnimals((activity as MainActivity).accessToken, 1)
val animalsResponse = animalsRequest.await()
val animalsResponse = petFinderService.getAnimals((activity as MainActivity).accessToken, 1)
if (animalsResponse.isSuccessful) {
animalsResponse.body()?.let {animalResult ->
if (animalResult.animals.size > 0) {
Expand Down Expand Up @@ -114,17 +112,14 @@ class RandomCompanionFragment : Fragment() {

view?.let {
petCaroselView = it.findViewById(R.id.petCarouselView)
petCaroselView.setViewListener(object : ViewListener {

override fun setViewForPosition(position: Int): View {
val carouselItemView = layoutInflater.inflate(R.layout.companion_photo_layout, null)
val imageView = carouselItemView.findViewById<ImageView>(R.id.petImage)
GlideApp.with(randomCompanionFragment).load(petPhotos[position])
.fitCenter()
.into(imageView)
return carouselItemView
}
})
petCaroselView.setViewListener { position ->
val carouselItemView = layoutInflater.inflate(R.layout.companion_photo_layout, null)
val imageView = carouselItemView.findViewById<ImageView>(R.id.petImage)
GlideApp.with(randomCompanionFragment).load(petPhotos[position])
.fitCenter()
.into(imageView)
carouselItemView
}
petCaroselView.pageCount = petPhotos.size
}
}
Expand Down
Loading

0 comments on commit b7136f0

Please sign in to comment.