To run integration tests in your trusted service, there are a number of dependencies that you need to make fun of and take care of. You cannot test all situations or the behavior of your service in such a way that it is difficult to replicate and trace FabricRuntime services (without writing your own FabricRuntime equivalence). It is also worth noting that it is not possible to run FabricRuntime without a cluster (including a local development cluster).
You also need to think about how perfect your integration tests are. For example, does your service access another service (including participants) within the same cluster using the transport technology (default communication model) that you want to include in your integration test? Do you need to make sure that the state is saved in several activations of the same service section?
First you need to get rid of all the hard dependencies before FabricRuntime (to things with dependencies on it), as well as the static support classes in your code:
Service / Actor Proxy
Do not use static ServiceProxy.Create<..)(..)> when calling other services, instead, make sure your Service accepts an IServiceProxyFactory instance in the constructor and uses this instance to create proxies to serve your service calls. The same applies to ActorProxy.Create<..>(..) , replace this with an instance of IActorProxyFactory . In program.cs where the service is built, enter new ServiceProxyFactory() and new ActorProxyFactory() . This is the easy part, now you need to mock so that your integration tests can actually create some form of proxy for downstream services. You also need to create some form of container (for example, a FabricRuntime layout) that stores instances of called services and participants. It is also difficult if you do not want to verify that the RunAsync method of your service performs a specific function. Beware of creating this static, but if you want to run it in a test runner, you do not want different tests to mix in the same container.
Service context
You need to make fun of your StatefulServiceContext and how your Service will be created. Service designers need to accept an instance of StatefulServiceContext to go to the base class, so you can create your own mocked context instances there when you create the service.
public StatefulService(StatefulServiceContext serviceContext) : base(serviceContext) {}
Service settings and activation context
You also need to find out if your service implementation is trying to read ICodePackageActivationContext or any parameters from the service manifest (as shown in this SO answer. Where do you set and access configuration parameters at runtime for a service environment? ). In this case, you need to replace it with your own mock version, and you also need to add this to the constructor. What you will find in most samples is a service context call, for example:
this.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");
If you do this in your service, you need to make sure that you have the StatefulServiceContext layout, as well as how your Service is created. When you register your service at runtime in Program.Main() , you get an instance of StatefulServiceContext in the register call:
ServiceRuntime.RegisterServiceAsync("ServiceType", context => new Service(context)).GetAwaiter().GetResult();
State
To mock the state and make it behave the same as when working in a real cluster, you need to mock the base handler for a reliable state: IReliableStateManagerReplica and you need to add an overloaded constructor to your services, which takes an instance of it and sends it to the base:
public StatefulService(StatefulServiceContext serviceContext, IReliableStateManagerReplica reliableStateManagerReplica) : base(serviceContext, reliableStateManagerReplica) {}
For participants in your IActorStateProvider you need to make fun of whether you want to handle state in your integration tests.
Summary
Depending on how advanced you want your integration tests to be and how close to the actual execution model you want, you can end up making fun and replace a large number of classes / interfaces. An example web link example https://github.com/Azure-Samples/service-fabric-dotnet-web-reference-app has some implementation of Mocks for the required classes, as well as https://github.com/loekd/ServiceFabric. Mocks contains Mocks for testing, although you may need to change the code if you really want to run integration tests , not just unit tests.