Testing

E2E testing

E2E testing (sending HTTP requests instead of running unit tests) for services made with waltid-service-commons is provided by waltid-service-commons-test.

To define the E2E tests:

@Test
fun e2e() = E2ETest.testBlock(
    config = ServiceConfiguration("e2e-test"),
    features = listOf(
        MyFeatureCatalog
    ),
    featureAmendments = mapOf(
        MyFeatureCatalog.myFeature to MyAmendment,
    ),
    init = {
        WaltidServices.minimalInit()
        Db.start()
        // ...
    },
    module = e2eTestModule,
    timeout = defaultTestTimeout
) {
    var client = testHttpClient()

    // Tests here:
    client.get("/some-api/some/endpoint").expectSuccess()
}

This runs the service-commons service (it will actually listen on a HTTP port), and the E2E test will actually have an HTTP client hit the API endpoints. This could happen like shown here:

val walletId = client.get("/wallet-api/wallet/accounts/wallets").expectSuccess()
    .body<AccountWalletInformation>().wallet.id.toString()

check(isValidWalletId(walletId)) { "Wallet id '$walletId' is invalid" }

See how simple that is? — Just standard Ktor HTTP requests.

Unit testing

We use kotlin.test as test framework.

Setup kotlin.test

  • Add the dependency to build.gradle.kts:
      dependencies {
          // Other dependencies.
          testImplementation(kotlin("test"))
      }
    
  • Add the test task to build.gradle.kts:
      tasks.test {
          useJUnitPlatform()
      }
    

Creating tests

See here