Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KointTestRule doesn't catch potential exceptions in tests #803

Closed
mottljan opened this issue May 8, 2020 · 1 comment · Fixed by #808
Closed

KointTestRule doesn't catch potential exceptions in tests #803

mottljan opened this issue May 8, 2020 · 1 comment · Fixed by #808

Comments

@mottljan
Copy link

mottljan commented May 8, 2020

Description
When the exception is thrown in the JUnit test which uses KoinTestRule, it fails other tests as well because the exception is not caught in the rule.

To Reproduce
I created a simple demo to illustrate the issue.

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class KoinTest {

    @get:Rule
    val koinRule = KoinTestRule.create {}

    @Test
    fun first() {
        throw Exception()
    }

    @Test
    fun second() {}
}

Second test should pass but fails with java.lang.IllegalStateException: A KoinContext is already started.

Here is the implementation in the KoinTestRule.

override fun apply(base: Statement, description: Description): Statement =
            object : Statement() {
                override fun evaluate() {
                    _koin = startKoin(appDeclaration = appDeclaration).koin
                    base.evaluate()
                    stopKoin()
                    _koin = null
                }
            }

base.evaluate() throws an exception from the test which is not caught and Koin is not properly stopped. Solution is to catch the exception or extend TestWatcher instead of TestRule because TestWatcher already handles exceptions internally.

Koin project used and used version
koin-test version 2.1.5

@arnaudgiuliani
Copy link
Member

Closing this. Continuing to PR 👍

arnaudgiuliani added a commit that referenced this issue May 28, 2020
* update changelog

* Updated dependency quote marks in setup docs (#735)

* remove build-log dump.txt (#724)

* Corrected `applicationContext` to `module` (#779)

Updated documentation as per issue #131 in change log

* Fix typo in Getting Started with Ktor (#796)

* Koin MP setup docv

* Fix KoinTestRule test exception handling. Fixes gh-803

Co-authored-by: Arnaud Giuliani <giuliani.arnaud@gmail.com>
Co-authored-by: Rajanikant Deshmukh <rajanikant.deshmukh@gmail.com>
Co-authored-by: Alexander Falb <221837+elexx@users.noreply.github.com>
Co-authored-by: Dacre Denny <dacre@mooce.co.nz>
Co-authored-by: Marcus Ramsden <marcus@bitsden.com>
Co-authored-by: Jan Mottl <jan.mottl@ackee.cz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants