I'm newbie in testing and received a notification from SonarCloud telling me that my DTO do not have any coverage, as it's just a class (a set of classes) I use for deserializing json I'm completely unaware how am I supposed to test them?
Dto definition:
using System.Text.Json.Serialization;
[JsonDerivedType(typeof(VehicleBaseDto), typeDiscriminator: "base")]
[JsonDerivedType(typeof(TrolleyDto), typeDiscriminator: "trolley")]
[JsonDerivedType(typeof(ScooterDto), typeDiscriminator: "scooter")]
[JsonDerivedType(typeof(ForkliftDto), typeDiscriminator: "forklift")]
public class VehicleBaseDto
{
public string Name { get; set; } = string.Empty;
public string CreationDate { get; set; }
}
public class TrolleyDto : VehicleBaseDto
{
public TrolleyOptions? ExtendedData { get; set; }
}
public class ScooterDto : VehicleBaseDto
{
public ScooterOptions? ExtendedData { get; set; }
}
public class ForkliftDto : VehicleBaseDto
{
public ForkliftOptions? ExtendedData { get; set; }
}
This an example of a deserialization test I wrote using xUnit:
[Fact]
public void TrolleyDto_Deserialization_Success()
{
var trolleyStr = @"{
""$type"":""trolley"",
""ExtendedData"":{
""Prop1"":""s/n"",
""Prop2"":""model"",
},
""Name"":""test name"",
""CreationDate"":""some value""
}";
var trolleyObj = JsonSerializer.Deserialize<VehicleBaseDto>(trolleyStr);
Assert.IsType<TrolleyDto>(trolleyObj);
}
I thought this usage of the DTO was enough for having some coverage on the DTO as the DTO don't have any methods to test, but SonarCloud says there is no coverage in any of its properties. What am I doing wrong?
ExcludeFromCodeCoverageAttribute.