Kotlin @RegisterExtension not working with salesforce kafka-junit5


#1

I need an embedded kafka for unit testing(UT) purposes.

I am trying to get github.com/salesforce/kafka-junit/tree/master/kafka-junit5 to work with Kotlin.

    companion object {
        /**
         * gleefully stolen from: https://github.com/salesforce/kafka-junit/tree/master/kafka-junit5
         *
         * We have a single embedded Kafka server that gets started when this test class is initialized.
         *
         * It's automatically started before any methods are run via the @RegisterExtension annotation.
         * It's automatically stopped after all of the tests are completed via the @RegisterExtension annotation.
         */
            @JvmField
            @RegisterExtension
            var sharedKafkaTestResource: SharedKafkaTestResource = SharedKafkaTestResource().withBrokers(2)
    
    }

but the BeforeAllCallback#beforeAll, AfterAllCallback#afterAll methods never get invoked.

  1. how can i get kafka-junit5 to work, or
  2. is there another embedded kafka solution that already works with kotlin?

here’s my pom:

    <properties>
    ... snip ...
        <salesforce-kafka-junit.version>3.1.0</salesforce-kafka-junit.version>
        <salesforce-kafka.version>1.1.1</salesforce-kafka.version>
        <junit-jupiter.version>5.3.2</junit-jupiter.version>
        <kotlin.version>1.2.71</kotlin.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>com.salesforce.kafka.test</groupId>
            <artifactId>kafka-junit5</artifactId>
            <version>${salesforce-kafka-junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.salesforce.kafka.test</groupId>
            <artifactId>kafka-junit-core</artifactId>
            <version>${salesforce-kafka-junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.11</artifactId>
            <version>${salesforce-kafka.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>${salesforce-kafka.version}</version>
        </dependency>
    ... snip ...
    </dependencies>

#2

I finally figured it out. It was a dependency problem. The following works, and now kafka-junit5 above works too:

    import org.assertj.core.api.Assertions.assertThat
    import org.junit.jupiter.api.Test
    import org.junit.jupiter.api.extension.AfterAllCallback
    import org.junit.jupiter.api.extension.BeforeAllCallback
    import org.junit.jupiter.api.extension.ExtensionContext
    import org.junit.jupiter.api.extension.RegisterExtension
    
    // @formatter:off
    /**
     * to validate that certain junit5 features work with kotlin
     *
     * pom.xml:
    
    <properties>
        <junit-jupiter.version>5.3.2</junit-jupiter.version>
        <junit-platform.version>1.3.2</junit-platform.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>${junit-platform.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
     *
     */
    // @formatter:on
    
    open class TestRegisterExtension {
    
        companion object {
    
            @JvmField
            @RegisterExtension
            val extension = BeforeAllAfterAllCallBackExtension()
        }
    
        @Test
        fun beforeAllCallbackTest() {
            println("beforeAllCallbackTest() called")
            Runtime.getRuntime().addShutdownHook( Thread() { afterAllCallbackTest() })
            assertThat(extension.beforeAllCalled).isEqualTo(true)
        }
    
        fun afterAllCallbackTest() {
            println("\nafterAllCallbackTest() called")
            assertThat(extension.afterAllCalled).isEqualTo(true)
        }
    
        open class BeforeAllAfterAllCallBackExtension
            : BeforeAllCallback, AfterAllCallback {
    
            var beforeAllCalled = false
            var afterAllCalled = false
    
            override fun beforeAll(context: ExtensionContext?) {
                println("beforeAll() called")
                beforeAllCalled = true
            }
    
            override fun afterAll(context: ExtensionContext?) {
                println("afterAll() called")
                afterAllCalled = true
            }
        }
    
    }