I am currently doing a integrationtest, but I am stuck on the following line in a function, that I need to test:


// left out code
var accessToken = new JwtSecurityToken(accessTokenString);

The accessToken is mocked using the Moq-framework and looks something like this:
accessTokenStringf9ececad-9b32-48b7-ae97-051afc440dba
But the problem is, I get the following message:
System.ArgumentException
IDX12741: JWT: ‘[PII is hidden]’ must have three segments (JWS) or five segments (JWE)

Is is somehow possible to mock the JwtSecurityToken? The outcome isn’t of any value for the test – I just sort of need to get past this line because that I want to test a specific part of the logic that comes afterwards.

Thanks in advance.
I’m not aware of any ways to mock a JwtSecurityToken although there probably are some. I should point out that for a proper integration test then you shouldn’t be mocking anything, you really want to test all the components running together.
Depending on your test framework you could have a test setup that gets a token before the test starts and then you can inject it? Or wrap the JwtSecurityToken in a factory class that you can mock?
New is glue. As long as your code explicitly instantiates a JwtSecurityToken, the only thing you can do is make a valid JWT and use it on your tests — there’s no way to bypass or mock new.
You shouldn’t be trying to mock anything other than external services in an integration test though, you usually want your application to be as close to production as possible for those.
For unit testing purposes, you’d usually try to hide cross-cutting concerns like authentication/authorization behind a service, which you can then mock when injecting into the class being tested. If the code you’re testing is the authentication service itself, then adding tests with valid and invalid tokens is the way to go.
C# devs
null reference exceptions

source