Skip to content

Allow writing extensions that utilize suspending lifecycle methods. #5032

@MarcusDunn

Description

@MarcusDunn

I would like to write extension classes implementing BeforeEachCallback, AfterAllCallback and BeforeAllCallback etc. using suspend functions, similar to what is supported when writing these callbacks with annotations. (as of JUnit 6)

suspend fun cleanDatabase() {
   // some suspend code
}

// not allowed
internal class DatabaseTestExtension : BeforeEachCallback {
    override suspend fun beforeEach(context: ExtensionContext) {
        cleanDatabase()
    }
}

// allowed
class DatabaseTest {
    @BeforeEach
    suspend fun beforeEach() {
        cleanDatabase()
    }
}

I would be happy if this was provided via different interfaces (i.e. SuspendBeforeEachCallback, SuspendAfterAllCallback, SuspendBeforeAllCallback, etc. and could be in a different package.

My specific use-case is doing database setup and teardown using r2dbc. I currently work around this with runBlocking which is an acceptable workaround.

internal class DatabaseTestExtension : BeforeEachCallback
    override fun beforeEach(context: ExtensionContext) = runBlocking {
        cleanDatabase()
    }
}

Deliverables

  • Allow writing extensions that have suspending callback functions.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions