diff --git a/core-kotlin/src/test/kotlin/com/baeldung/voidtypes/VoidTypesUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/voidtypes/VoidTypesUnitTest.kt new file mode 100644 index 0000000000..5c285c3135 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/voidtypes/VoidTypesUnitTest.kt @@ -0,0 +1,51 @@ +package com.baeldung.voidtypes + +import org.junit.jupiter.api.Test +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class VoidTypesUnitTest { + + fun returnTypeAsVoid(): Void? { + println("Function can have Void as return type") + return null + } + + fun unitReturnTypeForNonMeaningfulReturns(): Unit { + println("No meaningful return") + } + + fun unitReturnTypeIsImplicit() { + println("Unit Return type is implicit") + } + + fun alwaysThrowException(): Nothing { + throw IllegalArgumentException() + } + + fun invokeANothingOnlyFunction() { + alwaysThrowException() + + var name = "Tom" + } + + @Test + fun givenJavaVoidFunction_thenMappedToKotlinUnit() { + assertTrue(System.out.println() is Unit) + } + + @Test + fun givenVoidReturnType_thenReturnsNullOnly() { + assertNull(returnTypeAsVoid()) + } + + @Test + fun givenUnitReturnTypeDeclared_thenReturnsOfTypeUnit() { + assertTrue(unitReturnTypeForNonMeaningfulReturns() is Unit) + } + + @Test + fun givenUnitReturnTypeNotDeclared_thenReturnsOfTypeUnit() { + assertTrue(unitReturnTypeIsImplicit() is Unit) + } +} \ No newline at end of file