[.Net] Testing NoticeError API

Hi there.
I’m looking for some help in testing .Net Agent NoticeError API.
I’ve write some code that uses NoticeError(Exception exception) and wanted to unit test it. I’ve followed the following steps:

  1. Tried to creat a Mock (Moq) for NoticeError but it’s a static class, no game here;
  2. I would test the real thing anyway so, went ahead. Didn’t mock and used the real library but couldn’t see a thing in APM console;
  3. Put configurations line on app.config:
    <add key="NewRelic.AgentEnabled" value="true" />
    <add key="NewRelic.AppName" value="MyTestProject" />

And nothing happend…
4. Put NewRelic.Api.Agent.NewRelic.StartAgent(); before running the test and nothing…

I see on the console log the lines:
NewRelic.NoticeError(System.ApplicationException: NewRelicErrorLogger unit testing) <- the error i deliberated raised in the test

Then again, nothing on APM Console.
Does anyone haad this issue and have some tips to share?
I thank you for your time and attention

Why are you trying test mocking an external component that was tested an works and is doing the thing that suppouse to be doing?
I suggest to you the need to create a Stub component that simulates the NewRelic behavior.
If you try to start the agent to test you ar depending of external functionality of the same component or external components, so this sounds like an integration test.

I hope this helps


Thanks for the answer @santiago11

That’s exactly why i’m mocking up New Relic. Mock = Stub. I just want to test my code, not theirs.

Yes, that’s what was left. Yet, nothing is happening, no entry on APM console.
I’ve marked my tested method with [Transaction] and still nothing.

Ok, some updates for those who bumped here.
I’ve put the NoticeError call on a running project and it registered the exception. Seems like running tests from Test Manager is not considered a “running project”.
Still waiting for tips for unit testing codes that uses New Relic API.

There are a few diferences in the mock and stub uses and definitions
But trying to help a little bit, Could you add a newrelic no static wrapper that should build instances and allows the injection dependency and change the behavior?

You’re right @santiago11, they’re difference in definition, my bad.
Yes, wrapping it shall be a good alternative by now, but i’d really like avoid more code for that.
Anyway, thanks for the help. Let’s stay tunned for others’ experices.
Have a nice week!

1 Like