Kotlin Test framework integration for TestNG providing assertion implementations and annotation mappings
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-test-testng@1.9.0Kotlin Test TestNG provides seamless integration between Kotlin's multiplatform test framework (kotlin.test) and TestNG. It enables Kotlin tests to use familiar kotlin.test APIs while running on TestNG, bridging the gap between multiplatform test code and TestNG's advanced features like parameterized tests, test groups, and parallel execution.
implementation("org.jetbrains.kotlin:kotlin-test-testng:1.9.25")import kotlin.test.testng.TestNGAsserter
import kotlin.test.testng.TestNGContributorFor annotation imports (automatically available when using kotlin.test):
import kotlin.test.*import kotlin.test.*
class MyTestNGTest {
@Test
fun basicAssertionTest() {
assertEquals("Expected value", "actual value")
assertTrue(true)
assertNotNull("some value")
}
@BeforeTest
fun setUp() {
// Setup code before each test
}
@AfterTest
fun tearDown() {
// Cleanup code after each test
}
@Ignore
@Test
fun ignoredTest() {
// This test will be ignored
}
}Kotlin Test TestNG operates through a service provider pattern:
Automatic TestNG detection and asserter contribution for the kotlin.test framework.
/**
* Provides TestNGAsserter if org.testng.Assert is found in the classpath.
*/
class TestNGContributor : AsserterContributor {
/**
* Provides TestNGAsserter instance if TestNG is available, null otherwise.
* @return TestNGAsserter instance or null if TestNG is not in classpath
*/
fun contribute(): Asserter?
}Complete kotlin.test assertion implementation using TestNG's Assert class.
/**
* Implements kotlin.test assertions by delegating them to org.testng.Assert class.
*/
object TestNGAsserter : Asserter {
/**
* Asserts that the specified values are equal.
* @param message the message to report if the assertion fails
* @param expected the expected value
* @param actual the actual value
*/
fun assertEquals(message: String?, expected: Any?, actual: Any?)
/**
* Asserts that the specified values are not equal.
* @param message the message to report if the assertion fails
* @param illegal the value that should not equal actual
* @param actual the actual value
*/
fun assertNotEquals(message: String?, illegal: Any?, actual: Any?)
/**
* Asserts that the specified values are the same instance.
* @param message the message to report if the assertion fails
* @param expected the expected instance
* @param actual the actual instance
*/
fun assertSame(message: String?, expected: Any?, actual: Any?)
/**
* Asserts that the specified values are not the same instance.
* @param message the message to report if the assertion fails
* @param illegal the instance that should not be the same as actual
* @param actual the actual instance
*/
fun assertNotSame(message: String?, illegal: Any?, actual: Any?)
/**
* Asserts that the specified value is not null.
* @param message the message to report if the assertion fails
* @param actual the value to check for null
*/
fun assertNotNull(message: String?, actual: Any?)
/**
* Asserts that the specified value is null.
* @param message the message to report if the assertion fails
* @param actual the value to check for null
*/
fun assertNull(message: String?, actual: Any?)
/**
* Fails the current test with the specified message.
* @param message the message to report
*/
fun fail(message: String?): Nothing
/**
* Fails the current test with the specified message and cause exception.
* @param message the message to report
* @param cause the exception to set as the root cause of the reported failure
* @since Kotlin 1.4
*/
fun fail(message: String?, cause: Throwable?): Nothing
}Kotlin.test annotation mappings to TestNG equivalents, enabling cross-platform test code.
/**
* Maps kotlin.test @Test to TestNG @Test annotation
*/
typealias Test = org.testng.annotations.Test
/**
* Maps kotlin.test @Ignore to TestNG @Ignore annotation
*/
typealias Ignore = org.testng.annotations.Ignore
/**
* Maps kotlin.test @BeforeTest to TestNG @BeforeMethod annotation
*/
typealias BeforeTest = org.testng.annotations.BeforeMethod
/**
* Maps kotlin.test @AfterTest to TestNG @AfterMethod annotation
*/
typealias AfterTest = org.testng.annotations.AfterMethod/**
* Interface for contributing assertion implementations to kotlin.test framework
*/
interface AsserterContributor {
/**
* Provides Asserter instance or null depending on the current context
* @return asserter instance or null if not applicable
*/
fun contribute(): Asserter?
}
/**
* Interface defining assertion methods for kotlin.test framework
*/
interface Asserter {
fun fail(message: String?): Nothing
fun fail(message: String?, cause: Throwable?): Nothing // @SinceKotlin("1.4")
fun assertEquals(message: String?, expected: Any?, actual: Any?)
fun assertNotEquals(message: String?, illegal: Any?, actual: Any?)
fun assertSame(message: String?, expected: Any?, actual: Any?)
fun assertNotSame(message: String?, illegal: Any?, actual: Any?)
fun assertNull(message: String?, actual: Any?)
fun assertNotNull(message: String?, actual: Any?)
}import kotlin.test.*
class UserServiceTest {
private lateinit var userService: UserService
@BeforeTest
fun setUp() {
userService = UserService()
}
@Test
fun `should create user with valid data`() {
val user = userService.createUser("Alice", "alice@example.com")
assertNotNull(user)
assertEquals("Alice", user.name)
assertEquals("alice@example.com", user.email)
}
@Test
fun `should throw exception for invalid email`() {
assertFailsWith<IllegalArgumentException> {
userService.createUser("Bob", "invalid-email")
}
}
@AfterTest
fun tearDown() {
userService.cleanup()
}
}import kotlin.test.*
import org.testng.annotations.DataProvider
class CalculatorTest {
@DataProvider(name = "additionData")
fun additionDataProvider(): Array<Array<Any>> {
return arrayOf(
arrayOf(1, 2, 3),
arrayOf(-1, 1, 0),
arrayOf(0, 0, 0)
)
}
@Test(dataProvider = "additionData")
fun `should add numbers correctly`(a: Int, b: Int, expected: Int) {
val result = Calculator.add(a, b)
assertEquals(expected, result)
}
@Test(groups = ["unit"])
fun `should multiply correctly`() {
assertEquals(6, Calculator.multiply(2, 3))
}
}