Vaibhav Jaiswal
10/19/2024, 10:23 AMclass KotestAndKoin : FunSpec(), KoinTest {
override fun extensions() = listOf(KoinExtension(myKoinModule))
val userService by inject<UserService>()
init {
test("use userService") {
userService.getUser().username shouldBe "LeoColman"
}
}
}
But it does not work and crashes the test with KoinApplicationNotStarted Exception
This is a snippet of my test
class PostRepositoryTest : FunSpec(), KoinTest {
override fun extensions() = listOf(
KoinExtension(
modules = listOf(dataModule),
mockProvider = { mockkClass(it, relaxed = true) }
)
)
private val apiService = declareMock<FeedMediaApiService>()
private val socialRepo = declareMock<SocialRepository>()
private val activityApiService = declareMock<ActivityApiService>()
private val dataSource = declareMock<PostDataSource>()
private val userRepo = declareMock<UserRepository>()
private val postRepo by inject<PostRepository>()
This crashes, but if I change the vals to lateinit var
and initialize them in beforeTest
it works perfectly
StackTrace:
Caused by: java.lang.IllegalStateException: KoinApplication has not been started
at org.koin.core.context.GlobalContext.get(GlobalContext.kt:36)
at org.koin.core.component.KoinComponent$DefaultImpls.getKoin(KoinComponent.kt:33)
at org.koin.test.KoinTest$DefaultImpls.getKoin(KoinTest.kt:32)
at com.medial.app.data.repo.ugc.PostRepositoryTest.getKoin(PostRepositoryTest.kt:87)
at com.medial.app.data.repo.ugc.PostRepositoryTest.<init>(PostRepositoryTest.kt:514)
Is there anything wrong in my code, or is the documentation incorrect??Emil Kantis
10/19/2024, 11:22 AMVaibhav Jaiswal
10/19/2024, 11:27 AMVaibhav Jaiswal
10/21/2024, 5:13 AMclearAllMocks()
before each testEmil Kantis
10/22/2024, 11:57 AMdeclareMock
is not possible (compilation error), unless you add some definitely unwanted hack into your production code. Please provide a minimal sample if you want me to check it out further.Emil Kantis
10/22/2024, 11:58 AMVaibhav Jaiswal
10/22/2024, 12:06 PMEmil Kantis
10/22/2024, 12:19 PMVaibhav Jaiswal
10/22/2024, 12:29 PMEmil Kantis
10/22/2024, 12:41 PMVaibhav Jaiswal
10/22/2024, 12:42 PMEmil Kantis
10/22/2024, 12:42 PMlateinit var
, or overriding of test
Emil Kantis
10/22/2024, 12:43 PMVaibhav Jaiswal
10/22/2024, 12:45 PMinit
block a bit more readable
This decreases test time a lot, around 69% decrease in test time in 3 such basic tests
ThanksEmil Kantis
10/22/2024, 12:56 PMVaibhav Jaiswal
10/22/2024, 12:59 PMEmil Kantis
10/22/2024, 1:02 PMVaibhav Jaiswal
10/22/2024, 1:04 PMVaibhav Jaiswal
10/22/2024, 1:05 PMinit {
coroutineTestScope = true
beforeEach {
apiService = declareMock<UserApiService>()
preferences = declareMock<Preferences>()
dataSource = declareMock<PostDataSource>()
declare<List<Tracker>> { emptyList() }
declareMock<NetworkHelper>()
mockkObject(Analytics)
userRepo = get()
}
testGetUser()
testGetUserId()
testRefreshProfile()
testFetchUser()
testCreateUser()
testUpdateUser()
testUpdateFromLinkedin()
testCreateExperience()
testUpdateExperience()
testDeleteExperience()
testCreateEducation()
testUpdateEducation()
testDeleteEducation()
testStartOrgVerification()
testOrgVerification()
testClearAppData()
testClearDb()
testAnalytics()
}
And it takes a lot of time to complete as mocks are created inside beforeTest
Currently i have around 230 tests in my project and it takes around 1min to completeVaibhav Jaiswal
10/31/2024, 5:18 PM