akka/akka-core

Document ClusterSharding TestKit

Open

#31.227 geöffnet am 9. März 2022

Auf GitHub ansehen
 (4 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Scala (3.547 Forks)batch import
1 - triagedhelp wantedt:cluster-shardingt:docs

Repository-Metriken

Stars
 (13.277 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 17h 35m) (11 gemergte PRs in 30 T)

Beschreibung

Apologies in advance if this request is already implemented in some way but I didn't find anything in the doc/internet. My scenario is that I'm using akka persistence with akka cluster sharding for implementing a ES service.

The issue that I'm finding out is that I'm not able to basically test the classes that are using the sharding calls. For example, suppose I've the following class:

class MyService(sharding: ClusterSharding) {
     def method1(id: Int): Future[CustomObj]
}

where MyService.method1(id) is using the sharding object via the ask pattern.

My expected feature would be to have a simple way to mock the sharding responses.

The test that I would like (using Scalatest) is to write a mock on the responses generated by the sharding.

I would like to have something like this:

val sharding: MyClusterShardingMock = MyClusterShardingMock()
val subject = new MyService(sharding)

//define the mocking before call the class method
sharding.respondTo(req: expectedCommand)(response: expectedResponse)

subject.method1("123").futureValue mustBe response

Is there any way to achieve something similar to this? I'm in a point that I'm unable to unit testing some classes that depends from ClusterSharding. Thanks in advice.

Contributor Guide