- Community Build v25.5.0.107428
I have an asynchronous method that utilizes Polly’s ExecuteAsync and RabbitMQ client’s CreateChannelAsync to establish messaging channels:
public async Task MethodAsync<T>(T message) where T : IMessage
{
await retryPolicy.ExecuteAsync( async () =>
{
//Some code
await Method1Async();
await using var channel = await connection!.CreateChannelAsync();
switch ( sth.Type )
{
case sth.Type1:
await Method1Async( channel );
break;
case sth.Type2:
await Method2Async( channel );
break;
case sth.Type3:
case sth.Type4:
default:
throw new ArgumentOutOfRangeException( nameof( message ), "Invalid" );
}
} );
}
SonarQube reports that the line throw new ArgumentOutOfRangeException(nameof(message), “Invalid”)
; is only partially covered by tests.
However, I believe all scenarios are accounted for, including the default case, which is tested as follows:
[Theory]
[InlineData( sth.Type3)]
[InlineData( sth.Type4 )]
[InlineData( (sth)999 )]
[InlineData( (sth)(-1) )]
public async Task GivenMethodInvoked_WhenSthTypeIsInvalid_ThenExceptionThrown( sth type)
{
//some code
var ex = await Assert.ThrowsAsync<ArgumentOutOfRangeException>( async () => await class.MethodAsync( message ) );
Assert.Equal( "message", ex.ParamName );
Assert.Contains( "Invalid", ex.Message );
}
These tests are passing successfully, but I am getting that coverage issue.
And another important point to note:
This method previously passed coverage checks without issue.
Originally, it was a synchronous method, but we introduced asynchronous behavior to accommodate new requirements. The core logic, including the switch statement, remained unchanged - only async/await constructs were added.
The corresponding tests were also updated to reflect these changes. Given that the logic hasn’t changed, I don’t understand why SonarQube now flags the throw new ArgumentOutOfRangeException line as only partially covered.
Is there a known issue with SonarQube when dealing with async methods?