snowe
12/29/2023, 10:09 PMinclude
the testfactory. am I going to need to use some funky setup here or am I just missing something?snowe
12/29/2023, 10:10 PMclass DiskRecordingOneSourceApiTest : OneSourceApiTests {
override val uri by lazy { Uri.of("<http://localhost>:${servirtium.port()}") }
override val secret: String
get() = System.getenv("ONESOURCE_CLIENT_SECRET")
override val id: String
get() = System.getenv("ONESOURCE_CLIENT_ID")
private lateinit var servirtium: ServirtiumServer
@BeforeEach
fun start(info: TestInfo) {
servirtium = ServirtiumServer.Recording(
info.markdownName(),
ONE_SOURCE_LOGIN_URL,
Disk(File("src/test/resources/servitium-recordings")),
OneSourceInteractionOptions
)
servirtium.start()
}
@AfterEach
fun stop() {
servirtium.stop()
}
}
to this:
class DiskRecordingOneSourceApiKotestTest : FreeSpec({
lateinit var servirtium: ServirtiumServer
beforeEach {
servirtium = ServirtiumServer.Recording(
it.name.toString(),
ONE_SOURCE_LOGIN_URL,
Disk(File("src/test/resources/servitium-recordings")),
OneSourceInteractionOptions
)
servirtium.start()
}
val uri by lazy { Uri.of("<http://localhost>:${servirtium.port()}") }
include(oneSourceApiTests(uri, System.getenv("ONESOURCE_CLIENT_SECRET"), System.getenv("ONESOURCE_CLIENT_ID")))
afterEach {
servirtium.stop()
}
})
fun oneSourceApiTests(uri: Uri, secret: String, id: String) = freeSpec {
"login is a success" - {
val updateService = UpdateService(uri, HttpConfig().ssmClient())
val loginResult = updateService.performLogin(id, secret)
expectThat(loginResult)
.isA<Success<LoginResponseSuccess>>()
.get { this.value }
.get { success }
.isEqualTo("approved")
}
}
snowe
12/29/2023, 10:11 PM@JvmDefaultWithCompatibility
interface OneSourceApiTests {
val uri: Uri
val secret: String
val id: String
@Test
fun `login is a success`() {
val updateService = UpdateService(uri, HttpConfig().ssmClient())
val loginResult = updateService.performLogin(id, secret)
expectThat(loginResult)
.isA<Success<LoginResponseSuccess>>()
.get { this.value }
.get { success }
.isEqualTo("approved")
}
}
snowe
12/29/2023, 10:23 PMlateinit property servirtium has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property servirtium has not been initialized
at com.blah.login.servirtium.DiskRecordingOneSourceApiKotestTest$1$uri$2.invoke(DiskRecordingOneSourceApiKotestTest.kt:22)
where line 22 is the val uri by lazy { Uri.of("<http://localhost>:${servirtium.port()}") }
LeoColman
12/29/2023, 10:34 PMUri
object. It's not really lazy as you'll force an access to itLeoColman
12/29/2023, 10:34 PMLeoColman
12/29/2023, 10:35 PMval uri
inside beforeEach
snowe
12/29/2023, 10:35 PMLeoColman
12/29/2023, 10:36 PMservirtum
. I think you're trying to access it before you it's readyLeoColman
12/29/2023, 10:36 PMawaitStart
it would be even bettersnowe
12/29/2023, 10:38 PMsnowe
12/29/2023, 10:38 PMLeoColman
12/29/2023, 10:38 PMsnowe
12/29/2023, 10:38 PMsnowe
12/29/2023, 10:38 PMLeoColman
12/29/2023, 10:39 PMLeoColman
12/29/2023, 10:39 PMLeoColman
12/29/2023, 10:39 PMsnowe
12/29/2023, 10:39 PMsnowe
12/29/2023, 10:40 PMprepareSpec
or somethingLeoColman
12/29/2023, 10:40 PMbeforeEach
? If you're doing a one-time only setup, you can put it directly in the body of the specLeoColman
12/29/2023, 10:40 PMsnowe
12/29/2023, 10:40 PMsnowe
12/29/2023, 10:41 PMsnowe
12/29/2023, 10:42 PMLeoColman
12/29/2023, 10:42 PMLeoColman
12/29/2023, 10:42 PMbefore start
situation, as you're dealing with test names, for instanceLeoColman
12/29/2023, 10:43 PMsnowe
12/29/2023, 10:44 PMLeoColman
12/29/2023, 10:47 PMbeforeEach
as an extension, internally. However, an extension has more powersnowe
12/29/2023, 10:47 PMExtensions are reusable lifecycle hooks. In fact, lifecycle hooks are themselves represented internally as instances of extensions.and then on the lifecycle hooks page it says:
The second, related, method is to override the callback functions in the Spec. This is essentially just a variation on the first method.
snowe
12/29/2023, 10:47 PMsnowe
12/29/2023, 10:54 PMfun oneSourceApiTests(
recording: Boolean = false,
secret: String,
id: String
) = freeSpec {
lateinit var servirtium: ServirtiumServer
val uri by lazy { Uri.of("<http://localhost>:${servirtium.port()}") }
beforeEach {
servirtium = if (recording) {
ServirtiumServer.Recording(
it.name.toString(),
ONE_SOURCE_LOGIN_URL,
InteractionStorage.Disk(File("src/test/resources/servitium-recordings")),
OneSourceInteractionOptions
)
} else {
ServirtiumServer.Replay(
it.name.toString(),
InteractionStorage.Disk(File("src/test/resources/servitium-recordings")),
OneSourceInteractionOptions
)
}
servirtium.start()
}
afterEach {
servirtium.stop()
}
"login is a success" - {
val updateService = UpdateService(uri, HttpConfig().ssmClient())
val loginResult = updateService.performLogin(id, secret)
expectThat(loginResult)
.isA<Success<LoginResponseSuccess>>()
.get { this.value }
.get { success }
.isEqualTo("approved")
}
}
class DiskRecordingOneSourceApiKotestTest : FreeSpec({
include(oneSourceApiTests(true, System.getenv("ONESOURCE_CLIENT_SECRET"), System.getenv("ONESOURCE_CLIENT_ID")))
})
snowe
12/29/2023, 10:54 PMsnowe
12/29/2023, 10:58 PMsnowe
12/29/2023, 11:01 PMsnowe
12/29/2023, 11:02 PMMervyn McCreight
12/30/2023, 2:53 AMsnowe
12/30/2023, 2:54 AMMervyn McCreight
12/30/2023, 2:57 AMval uriFn = { Uri.of(...) }
And then pass this one into your contract.
You'd then have no need to pass around the servirtium object, or am I missing something? 🤔snowe
12/30/2023, 2:59 AMMervyn McCreight
12/30/2023, 3:05 AM() -> Uri
, and thus could just be invoked.
Okay, seems like this is more complicated, reading your last comment :Dsnowe
12/30/2023, 3:06 AMLeoColman
12/30/2023, 12:31 PMLeoColman
12/30/2023, 12:31 PMLeoColman
12/30/2023, 12:45 PMsnowe
01/02/2024, 5:26 PMsnowe
01/02/2024, 5:26 PM