# Mock an APM instance

**URL:** <https://discuss.elastic.co/t/mock-an-apm-instance/224686>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [March 23, 2020, 2:44pm UTC](https://discuss.elastic.co/t/mock-an-apm-instance/224686 "2020-03-23T14:44:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spark](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@spark](https://discuss.elastic.co/u/spark)\
**Post date:** [March 23, 2020, 2:44pm UTC](https://discuss.elastic.co/t/mock-an-apm-instance/224686/1 "2020-03-23T14:44:44Z")

</div>

Hey,

I'm using Kibana, ElasticSearch and APM in version 7.5.1.

My project is a C# project with `Elastic.APM` in version `1.4.0` installed via nuget.  
I want to mock the elastic apm instance when doing unit testsing.

What would be the right approach to mock the Agent and check the outcome (transactions and spans)?

I had a look at [https://github.com/elastic/apm-agent-dotnet/blob/master/test/Elastic.Apm.Tests/TransactionSamplingTests.cs](https://github.com/elastic/apm-agent-dotnet/blob/master/test/Elastic.Apm.Tests/TransactionSamplingTests.cs), however due to the protection level I cannot use the mock classes `MockPayloadSender` or `MockConfigSnapshot` in its original form.

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [March 27, 2020, 9:21pm UTC](https://discuss.elastic.co/t/mock-an-apm-instance/224686/2 "2020-03-27T21:21:12Z")

</div>

Hi @spark,

we actually did not think that `MockPayloadSender` or `MockConfigSnapshot` would be something that our users would use.

I think when you do unit tests, you don't actually need to test the agent.

So, if you have a class where you for example use the public agent API, you can just pass an `ITracer` instance into your code - in production this would come from the real agent and in a unit test you can just pass null.

Like here:

```auto
public void MyMethod(ITracer tracer)
{
	// If ITracer is a real tracer, it'll start the transaction, if ITracer is null, nothing will happen here 
	var transaction = tracer?.StartTransaction("SampleTransaction", "SampleSpan");
	try
	{
		// your code with here
	}
	catch (Exception e)
	{
		transaction?.CaptureException(e);
		throw;
	}
	finally
	{
		transaction?.End();
	}
}

```

In prod. code you can pass `Agent.Tracer` into this method, in a unit test just leave it to `null`. The concept is the same if you unit test a class - you can e.g. dependency inject in the constructor.

---

<div class="post-metadata">

**Author:** ![spark](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@spark](https://discuss.elastic.co/u/spark)\
**Post date:** [March 31, 2020, 2:48pm UTC](https://discuss.elastic.co/t/mock-an-apm-instance/224686/3 "2020-03-31T14:48:34Z")

</div>

That works too.  
I originally ended up creating a custom ElasticConfigurationReader with an invalid URL, but yours is the prettier solution.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 21, 2020, 10:48am UTC](https://discuss.elastic.co/t/mock-an-apm-instance/224686/4 "2020-04-21T10:48:45Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
