https://kotlinlang.org logo
#kotest
Title
# kotest
d

dave08

03/23/2021, 8:58 AM
Boy... now I'm confused... that test passed even though I have a
TODO()
in the subject's function being called... by stepping through the code, I see that code is reached, but the test passes... 🤯
l

LeoColman

03/23/2021, 10:19 AM
which test?
Could you show us an example?
d

dave08

03/23/2021, 10:23 AM
A very simple one (I had it a bit more complex... but I think this should do):
Copy code
@RobolectricTest
class SyncAuthenticationTest : FreeSpec() {

    init {
		"A device with an account" - {
			"Use the account info to get an access token with a direct grant" {
				val subject = SomeObject(account)

				val result = subject.getAuthToken()

				result shouldBe "-- access token --"
			}
		}
	}
}

class SomeObject {
    fun getAuthToken(): String { TODO() }
}
using 0.3.0.3-SNAPSHOT of the robolectric extension
l

LeoColman

03/23/2021, 10:26 AM
It fails correctly here
kotlin.NotImplementedError: An operation is not implemented.
Copy code
@RobolectricTest
class Testit : FreeSpec({
    "A device with an account" - {
        "Use the account info to get an access token with a direct grant" {
            val subject = SomeObject()
            val result = subject.getAuthToken() 
            result shouldBe "-- access token --"
        }
    }
})

class SomeObject {
    fun getAuthToken(): String { TODO() }
}
d

dave08

03/23/2021, 10:27 AM
Maybe when I mixed in the Wiremock extension?
Copy code
private val syncApi = WireMockServer(
	private var syncApi: Lazy<WireMockServer> = lazy { WireMockServer(
			WireMockConfiguration.options()
					.port(30111)
					.notifier(ConsoleNotifier(true))
	)
	private val am = AccountManager.get(getApplicationContext())
	) }

	private lateinit var am: AccountManager

	init {
		listener(WireMockListener(syncApi, ListenerMode.PER_SPEC))
		beforeSpec {
			am = AccountManager.get(getApplicationContext())
		}

		listener(LazyWireMockListener(syncApi, ListenerMode.PER_SPEC))
Copy code
class LazyWireMockListener(
      private val server: Lazy<WireMockServer>,
      private val listenerMode: ListenerMode
) : TestListener {

   override suspend fun beforeTest(testCase: TestCase) {
      if (listenerMode == ListenerMode.PER_TEST) {
         server.value.start()
      }
   }

   override suspend fun afterTest(testCase: TestCase, result: TestResult) {
      if (listenerMode == ListenerMode.PER_TEST) {
         server.value.stop()
      }
   }

   override suspend fun beforeSpec(spec: Spec) {
      if (listenerMode == ListenerMode.PER_SPEC) {
         server.value.start()
      }
   }

   override suspend fun afterSpec(spec: Spec) {
      if (listenerMode == ListenerMode.PER_SPEC) {
         server.value.stop()
      }
   }

   companion object {
      fun perSpec(wireMockServer: Lazy<WireMockServer>) = LazyWireMockListener(wireMockServer, ListenerMode.PER_SPEC)

      fun perTest(wireMockServer: Lazy<WireMockServer>) = LazyWireMockListener(wireMockServer, ListenerMode.PER_TEST)
   }
}

enum class ListenerMode {
   PER_TEST,
   PER_SPEC
}
I had even removed the account manager part...
I would try to get back to that state myself, I'm just in the middle of a few changes... if you don't manage now, I'll try reproducing it a bit later. Thanks!
2 Views