How do we test views using integration tests?


I have been trying to find a way to test views. Since I do not find any example yet I tried using integration test to test things out. However when I run the test, I do get the error:

{“timestamp”:“2022-12-11T08:04:35.204Z”,“thread”:“”,“logger”:“kalix.javasdk.impl.DiscoveryImpl”,“message”:“Error reported from Kalix system: KLX-00121 An incoming event for the view [CustomerByCityStreaming] was missing the subject id attribute ce-subject, view updates are stalled.\n\nEach update passed to a view must have a subject id, used as a primary key in the view, each unique subject-id can only have one entry in the view, if the update comes from a message broker the subject id is defined with the cloud event attribute [ce-subject]. \n\nAt customer/view/customer_view.proto:134:1:\nservice CustomerByCityStreaming {\n option (kalix.codegen) = {\n view: {}\n };\n\n // update methods omitted”,“context”:“default”,“severity”:“ERROR”}

What I had in the test was:

private val viewClient = testKit.getGrpcClient(classOf[CustomerByCityStreaming])

  val result =
    viewClient.processCustomerCreated(CustomerCreated(Some(CustomerState("1", "", "name", None))))

  result.futureValue shouldBe Customer("1", "", "name", None)

Is there any example that I can follow or how do we properly test view code? Thanks for your help.

Hi @wpoosanguansit it looks like you are trying to test a view based on the entity events stream (or some Although it is possible from the gRPC perspective, you shouldn’t send events by hand in this case. This might corrupt the view state. There is no ViewTestKit yet, but you can stay with integration tests. Simply run some command against your entity, to produce expected events, “consumed” later by the view.

Some examples of a ValueEntity IT tests: kalix-jvm-sdk/ShoppingCartServiceIntegrationSpec.scala at main · lightbend/kalix-jvm-sdk · GitHub

1 Like

Thank you. I will check that out.

Hi @aludwiko , Do you have an example of an integration test for a View?

I have no issues in writing integration tests for Value and EventSourced entities. However, when updating the entity, I’m just not able to query the View and get the latest state. The View is empty and so I keep getting “NOT_FOUND” back. If I start the services manually and do the same operations by hand I can get the latest state from the View!

While it should work in theory, it would help a lot to see an actual integration test that works in practice.

Thank you.

PS: The issue was that I was querying the View state too fast. So I had to wait a bit (e.g. Thread.sleep(1000)) and then query.

PS: The issue was that I was querying the View state too fast. So I had to wait a bit (e.g. Thread.sleep(1000)) and then query.

You can also use some utility like await() and configure how many times to retry and with what delay. See example here.

Thank you for the example @efgpinto. Yes, I was using something similar: eventually.

class ViewIntegrationSpec ... with ScalaFutures with Eventually with IntegrationPatience {
  eventually {
    val response = view.query(...).futureValue
    response shouldBe ...
1 Like